我的模型:EF-代码首先复杂类型的导航性能
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; }
}
当生成数据库,我得到:
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.
如何我有一个复杂型内航行的财产?如果我删除国家航行财产,它工作正常。
但是'LocationID'类里的'CountryID'整数怎么样?是否有可能使外键约束? (我有类似的问题,不能让它工作) –
@伊萨克:不,这是不可能的。如果你想在数据库中使用FK,你必须直接在数据库中执行它,但是EF不会反映它。 –
直接在MSDN的复杂类型描述中提到了这种不支持的事实:http://msdn.microsoft.com/en-us/library/bb738472.aspx –