EF-Code具有导航属性的第一个复杂类型 [英] EF-Code first complex type with a navigational property

查看:90
本文介绍了EF-Code具有导航属性的第一个复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型:

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.

推荐答案

复杂类型的导航属性(参考其他实体)不支持。您必须使用位置实体(拥有自己的表格),或从<$ c中删除导航属性 Country $ c>位置(并添加由Steve Morgan提到的 [ComplexType] 属性。

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).

修改

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


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

"Complex type cannot contain navigation properties."

这篇关于EF-Code具有导航属性的第一个复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆