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

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


The following are my classes:


public class Landlord : UserProfile
    public static int LandlordProfileViews { get; set; }

    // A Landlord can have many ResidentialProperties
    public virtual ICollection<ResidentialProperty> ResidentialProperties { get; set; }



public class ResidentialProperty
    public int ResidentialPropertyId { get; set; }
    // ...

    // A ResidentialProperty has 1 Landlord
    public int UserId { get; set; }
    public virtual UserProfile UserProfile { get; set; }

The association is one-to-many since a landlord may own numerous residential properties. I'm attempting to use the database to add test data.Seed() method. The issue I have is that I'm unsure of how to describe each end of the relationship in the method. I tried the following things:

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) generates an error saying that type ResidentialProperty cannot be implicitly converted to ICollection ResidentialProperty >.

How may one-to-many relationships be implemented in the Seed() method?


To try to implement this kind of relationship, I've added the following to my context class: An owner of residential property may own several of them. There can only be one landlord for a residential property.:

            .HasMany(x => x.ResidentialProperties)
            .HasForeignKey(x => x.ResidentialPropertyId);

            .HasRequired(x => x.UserProfile);

I continue to experience this issue:

\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'..

I'm still unsure of what I'm doing incorrectly.

2/18/2013 3:27:50 PM

Accepted Answer

A list of ResidentalProperties must be returned. the issue you are askingResidentialProperties = residentialProperties.FirstOrDefault(x => x.ResidentialPropertyId == 1) just returns one ResidentialProperty asset. Just actResidentialProperties = residentialProperties

Edit: A single configuration can be used to set up many to one. Additionally, you need 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);
2/18/2013 2:32:52 PM

Popular Answer


Related Questions


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