Entity Framework - implementing one-to-many foreign key relationship in Seed() method

c# ef-code-first ef-migrations entity-framework seeding

Question

I have the following classes:

Landlord

[Table("Landlord")]
public class Landlord : UserProfile
{
    public static int LandlordProfileViews { get; set; }

    // A Landlord can have many ResidentialProperties
    [ForeignKey("ResidentialPropertyId")]
    public virtual ICollection<ResidentialProperty> ResidentialProperties { get; set; }

}

ResidentialProperty

[Table("ResidentialProperty")]
public class ResidentialProperty
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ResidentialPropertyId { get; set; }
    // ...

    // A ResidentialProperty has 1 Landlord
    [ForeignKey("UserId")]
    public int UserId { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}

A Landlord can have many ResidentialProperties, so the association is one-to-many. I'm trying to add test data to my database using the Seed() method. My problem is I don't know how to define the many end of the relationship in the method. The following is what I've tried:

var residentialProperties = new List<ResidentialProperty>
{
    // Property 1 associated with LandLord 1
    new ResidentialProperty { /* properties */ },
    // ...
 }

var landlords = new List<Landlord>
 {
    new Landlord { /* properties */ ResidentialProperties = residentialProperties.FirstOrDefault(x => x.ResidentialPropertyId == 1) },
    // ...
 }

The ResidentialProperties = residentialProperties.FirstOrDefault(x => x.ResidentialPropertyId == 1) gives a 'Cannot implicitly convert type ResidentialProperty to ICollection < ResidentialProperty > error.

How do you implement one-to-many relationships in the Seed() method?

EDIT:

I've added the following in my context class to try and implement this type of relationship: A Landlord can have many ResidentialProperties. A ResidentialProperty can only have one Landlord:

modelBuilder.Entity<Landlord>()
            .HasMany(x => x.ResidentialProperties)
            .WithRequired()
            .HasForeignKey(x => x.ResidentialPropertyId);

modelBuilder.Entity<ResidentialProperty>()
            .HasRequired(x => x.UserProfile);

I'm still getting this error:

\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Landlord_ResidentialProperties_Target' in relationship 'Landlord_ResidentialProperties'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'..

Still at a loss as to what I'm doing wrong.

1
1
2/18/2013 3:27:50 PM

Accepted Answer

You need to return a list of ResidentalProperties. The query you have ResidentialProperties = residentialProperties.FirstOrDefault(x => x.ResidentialPropertyId == 1) is only returning a single ResidentialProperty enitity. Just do ResidentialProperties = residentialProperties

Edit: You can do the many to one setup through a single configuration. You also have to specify the foreign key.

            //ResidentialProperty has 1 Landlord ,
            //Landlord has many ResidentialProperties
            modelBuilder.Entity<ResidentialProperty>().HasRequired(a=> a.UserProfile)
                .WithMany(c=> c.ResidentialProperties)
                .HasForeignKey(a=> a.UserId);
3
2/18/2013 2:32:52 PM

Popular Answer

I think this is the model relationships you are asking for.

public class Landlord : UserProfile
{
    [Key]
    public Guid Id {get;set;} //If you named this "LandlorId" you wouldn't need the [Key]

//this convention will wire up to the otherside    
public virtual ICollection<ResidentialProperty> ResidentialProperties { get; set; }

}

public class ResidentialProperty{
   [Key]
   public Guid Id {get;set;}

   //this convention will wire up to the otherside
   public LandLordId {get;set;}
   public Landlord Landlord {get;set;}
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow