Entity framework EF映射问题
假设我有以下课程:Entity framework EF映射问题,entity-framework,mapping,ef-code-first,Entity Framework,Mapping,Ef Code First,假设我有以下课程: public class User { public int UserId { get;set;} public string Firstname { get;set;} public Interests Interests { get;set;} } public class Interests { public IList<Team> FavoriteTeams { get;set;} public IList<F
public class User
{
public int UserId { get;set;}
public string Firstname { get;set;}
public Interests Interests { get;set;}
}
public class Interests
{
public IList<Team> FavoriteTeams { get;set;}
public IList<Food> FavoriteFoods { get;set;}
}
public class Team{
public int TeamId { get;set;}
public string City {get;set;}
public string Name { get;;set;}
}
public class Food{
public int FoodId { get;set;}
public string FoodName { get;set;}
}
公共类用户
{
public int UserId{get;set;}
公共字符串名{get;set;}
公共利益{get;set;}
}
公共阶级利益
{
公共IList FavoriteTeams{get;set;}
公共IList FavoriteFoods{get;set;}
}
公开课小组{
public int TeamId{get;set;}
公共字符串City{get;set;}
公共字符串名称{get;;set;}
}
公共食品{
公共int{get;set;}
公共字符串FoodName{get;set;}
}
在数据库级别,我希望有以下表:
使用者
团队
食品
用户团队
用户食品
基本上,我希望有用户实体的兴趣属性,但没有一个单独的表
我使用的是EF4.1代码优先的POCO类。我知道最简单的方法是将团队和食物收集作为用户的一个属性,这是可以的,但是从对象模型的角度来看,我希望它是它自己的属性。知道我将如何实现这种映射吗?我会说:您正在寻找的映射是不可能的,因为: 如果不希望类
兴趣
映射到表,则该类不能是实体,并且用户
类中的兴趣
属性不能是导航属性。为避免EF将Interests
属性解释为导航属性,您只有两个选项:
- 将其标记为
->没有帮助,因为您将完全失去从NotMapped
到用户
和团队
的关系食品
- 将
类标记为兴趣
->在您的示例中不可能,因为复杂类型是ComplexType
这只是我的理解水平。我不太可能忽略了另一个选项。很遗憾,EF不支持复杂类型上的导航属性,这是竞争框架从一开始就支持的。我希望我能说我很惊讶。。。