EF-Code第一个具有导航属性的复杂类型

Sha*_*ean 12 entity-framework ef-code-first entity-framework-4.1

我的型号:

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; }

}
Run Code Online (Sandbox Code Playgroud)

生成数据库时,我得到:

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.
Run Code Online (Sandbox Code Playgroud)

如何在复杂类型中拥有导航属性?如果我删除国家导航属性,它工作正常.

Sla*_*uma 10

不支持复杂类型的导航属性(引用其他实体).您必须创建Location一个实体(使用自己的表)或Country从中删除导航属性Location(并添加[ComplexType]Steve Morgan提到的属性).

编辑

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

"复杂类型不能包含导航属性."