Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF-Code first complex type with a navigational property

My Model:

public class Country
{
    public int CountryId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

public class Location
{
    public string Address { get; set; }

    public virtual int CountryId { get; set; }
    public virtual Country Country { get; set; }
}    

public class User{

    protected User()
    {
        Location = new Location();
    }

    public int UserId { get; set; }
    public Location Location { get; set; }

}

When generating the database, I get:

One or more validation errors were detected during model generation:

System.Data.Edm.EdmEntityType: : EntityType 'Location' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Locations� is based on type �Location� that has no keys defined.

How do I have a navigational property inside a complex type? If I remove the country navigational property, it works fine.

like image 305
Shawn Mclean Avatar asked Sep 29 '11 20:09

Shawn Mclean


1 Answers

Navigation properties (refering to other entities) on a complex type are not supported. You must either make your Location an entity (with its own table) or remove the navigation property Country from Location (and add the [ComplexType] attribute as mentioned by Steve Morgan).

Edit

Reference: http://msdn.microsoft.com/en-us/library/bb738472.aspx

"Complex type cannot contain navigation properties."

like image 81
Slauma Avatar answered Sep 18 '22 20:09

Slauma