EF映射问题
假设我有以下类:
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;}
}
在数据库级别,我希望有以下表:
用户 团队 食品 用户团队 UserFoods
基本上我希望拥有用户实体的 Interests 属性,但没有单独的表。
我正在使用 EF 4.1 代码优先 POCO 类。我知道最简单的方法是将 Teams 和 Food 集合作为 User 的属性,这是可以的,但从对象模型的角度来看,我希望它成为它自己的属性。知道如何实现这种类型的映射吗?
Lets say I have the following classes:
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;}
}
At the database level I would like to have the following tables:
Users
Teams
Foods
UserTeams
UserFoods
Basically I would like to have the Interests property of the user entity but not have a seperate table for it.
I am using EF 4.1 code first POCO classes. I know the easiest thing to do is put the Teams and Food collection as a property on the User, which is ok, but from the object model point of view I would like it be be its own property. Any idea of how I would acheive this type of mapping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说:您正在寻找的映射是不可能的,因为:
如果您不希望类
Interests
映射到表,它一定不能是一个实体,并且
属性不得是导航属性。为了避免 EF 将User
类中的 InterestsInterests
属性解释为导航属性,您只有两个选择:NotMapped
->没有帮助,因为您将失去从User
到Team
和Food
的关系,完全将Interests
类标记为 <代码>复杂类型 ->在您的示例中不可能,因为复杂类型不允许包含导航属性 。这只是从我的理解水平来看。我不太可能忽略了另一个选择。
I would say: The mapping you are looking for is not possible, because:
If you don't want the class
Interests
to be mapped to a table, it must not be an entity and theInterests
property in yourUser
class must not be a navigation property. To avoid that EF interprets theInterests
property as a navigation property, you would only have two options:NotMapped
-> Does not help because you would lose the relationship fromUser
toTeam
andFood
completelyInterests
class asComplexType
-> Not possible in your example because complex types are not allowed to contain navigation properties.That's just from my level of understanding. It's not unlikely that I overlooked another option.