EF映射问题

发布于 2024-11-03 23:05:45 字数 755 浏览 0 评论 0原文

假设我有以下类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-11-10 23:05:45

我想说:您正在寻找的映射是不可能的,因为:

如果您不希望类 Interests 映射到表,它一定不能是一个实体,并且 User 类中的 Interests 属性不得是导航属性。为了避免 EF 将 Interests 属性解释为导航属性,您只有两个选择:

  • 将其标记为 NotMapped ->没有帮助,因为您将失去从 UserTeamFood 的关系,完全将
  • 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 the Interests property in your User class must not be a navigation property. To avoid that EF interprets the Interests property as a navigation property, you would only have two options:

  • Mark it as NotMapped -> Does not help because you would lose the relationship from User to Team and Food completely
  • Mark the Interests class as ComplexType -> 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文