如何保存 ICollection与 EF CTP5

发布于 2024-10-30 21:28:24 字数 450 浏览 1 评论 0原文

我的班级有以下属性:Team

[Key]
public virtual long Id { get; set; }

public Guid ClubIdentifier { get; set; }
public GenderEnum Gender { get; set; }
public TeamAgeCategoryEnum TeamAgeCategory { get; set; }
public ICollection<int> BirthYears { get; set; }

如何将属性 BirthYears 中的内容保存到我的数据库中,我让 EF 根据模型创建我的数据库,但属性 BirthYears 被遗漏在我的数据库中。我本来期望有一个新表来保存 int 值和我的团队 ID 值。

我错过了什么,我想我需要在我的存储库类中执行一些 OnModelCreating 方法。

I have this properties for my class: Team

[Key]
public virtual long Id { get; set; }

public Guid ClubIdentifier { get; set; }
public GenderEnum Gender { get; set; }
public TeamAgeCategoryEnum TeamAgeCategory { get; set; }
public ICollection<int> BirthYears { get; set; }

How can I save the content in property BirthYears to my database, I am letting the EF create my database based on the model, but the property BirthYears is left out in my database. I would have expected a new table that hold the int-value and my Team Id value.

What have I missed, I guess I need to do something OnModelCreating method in my repository class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只为守护你 2024-11-06 21:28:25

如果您查看 EntityTypeConfiguration 类,您将看到以下用于定义一对多关系的签名(这是 TeamTeam 之间的关系) code>BirthYears):

HasMany<TTargetEntity>(Expression<Func<TEntityType, ICollection<TTargetEntity>>>
   navigationPropertyExpression) where TTargetEntity : class;

如您所见,有一个约束 where TTargetEntity : class 要求 BirthYearsclass 的集合代码>对象。 int 不是一个类,因此不可能进行映射。

我能看到的唯一解决方法是定义一个小类……

public class BirthYear
{
    public int Id { get; set; }
    public int Value { get; set; }
}

然后在 Team 类的集合中使用它:

public ICollection<BirthYear> BirthYears { get; set; }

映射约定应该自动创建一对多关系,这样您就不会需要 Fluent API 来设置关联。

编辑

根据Ladislav在评论中的正确评论家进行更正:

BirthYear需要一个额外的Key属性。我添加了一个属性 Id

另外,我猜测 BirthYears 将是依赖于 Team 的属性。映射约定将创建从 BirthYearTeam 的可选关系。我认为模型更适合使用 Fluent API 来建立这种关系:

modelBuilder.Entity<Team>()
            .HasMany(t => t.BirthYears)
            .WithRequired();

这将自动启用级联删除 - 当删除团队时,关联的 BirthYears 将从数据库中删除。

编辑 2

(再次基于 Ladislav 的评论)如果您不想复制 BirthYears 表中的年份,您还可以设置多对多关系:

modelBuilder.Entity<Team>()
            .HasMany(t => t.BirthYears)
            .WithMany();

这将添加一个联接表(< code>TeamBirthYears) 将 TeamBirthYear 之间的内容存储到数据库中。从存储空间或性能的角度来看,您可能不会赢得任何东西(因为 BirthYear 类非常小,并且 BirthYear 表中的记录的大小与记录在连接表中)。但如果您打算迟早通过附加属性扩展 BirthYear 类,这可能是更好的方法。否则,我个人会保持一对多关系的简单性。但选择权在你。

If you take a look at the EntityTypeConfiguration<TEntityType> class you will see the following signature for defining a one-to-many relationship (which is your relation between Team and BirthYears):

HasMany<TTargetEntity>(Expression<Func<TEntityType, ICollection<TTargetEntity>>>
   navigationPropertyExpression) where TTargetEntity : class;

As you can see, there is a constraint where TTargetEntity : class which requires that BirthYears is a collection of class objects. int isn't a class, so the mapping won't be possible.

The only workaround I can see is to define a little class ...

public class BirthYear
{
    public int Id { get; set; }
    public int Value { get; set; }
}

... and then use this in your collection in the class Team:

public ICollection<BirthYear> BirthYears { get; set; }

The mapping conventions should automatically create a one-to-many relationship, so that you don't need the Fluent API to set up the association.

Edit

Correction according to Ladislav's correct critic in the comments:

The class BirthYear needs an additional Key property. I've added a property Id.

Also I guess that BirthYears will be a property dependent on Team. The mapping conventions will create an optional relationship from BirthYear to Team. I think it would be more suited for the model to make this relationship required by using the Fluent API:

modelBuilder.Entity<Team>()
            .HasMany(t => t.BirthYears)
            .WithRequired();

This will automatically enable cascading delete - the associated BirthYears will be deleted from the database when a Team is deleted.

Edit 2

(Again based on Ladislav's comment) If you don't want to replicate the years in the BirthYears table you could also setup a Many-To-Many relationship:

modelBuilder.Entity<Team>()
            .HasMany(t => t.BirthYears)
            .WithMany();

This will add a join table (TeamBirthYears) between Team and BirthYear into the database. From the viewpoint of storage space or performance you wouldn't probably win anything (since the BirthYear class is very small and a record in the BirthYear table has the same size as a record in the join table). But it might be a better approach if you have in mind to extend the BirthYear class by additional properties sooner or later. Otherwise I would personally keep it simple with the One-To-Many relationship. But the choice is yours.

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