如何保存 ICollection与 EF CTP5
我的班级有以下属性: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看
EntityTypeConfiguration
类,您将看到以下用于定义一对多关系的签名(这是Team
和Team
之间的关系) code>BirthYears):如您所见,有一个约束
where TTargetEntity : class
要求BirthYears
是class
的集合代码>对象。int
不是一个类,因此不可能进行映射。我能看到的唯一解决方法是定义一个小类……
然后在 Team 类的集合中使用它:
映射约定应该自动创建一对多关系,这样您就不会需要 Fluent API 来设置关联。
编辑
根据Ladislav在评论中的正确评论家进行更正:
类
BirthYear
需要一个额外的Key属性。我添加了一个属性Id
。另外,我猜测
BirthYears
将是依赖于Team
的属性。映射约定将创建从BirthYear
到Team
的可选关系。我认为模型更适合使用 Fluent API 来建立这种关系:这将自动启用级联删除 - 当删除团队时,关联的 BirthYears 将从数据库中删除。
编辑 2
(再次基于 Ladislav 的评论)如果您不想复制 BirthYears 表中的年份,您还可以设置多对多关系:
这将添加一个联接表(< code>TeamBirthYears) 将
Team
和BirthYear
之间的内容存储到数据库中。从存储空间或性能的角度来看,您可能不会赢得任何东西(因为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 betweenTeam
andBirthYears
):As you can see, there is a constraint
where TTargetEntity : class
which requires thatBirthYears
is a collection ofclass
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 ...
... and then use this in your collection in the class Team:
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 propertyId
.Also I guess that
BirthYears
will be a property dependent onTeam
. The mapping conventions will create an optional relationship fromBirthYear
toTeam
. I think it would be more suited for the model to make this relationship required by using the Fluent API: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:
This will add a join table (
TeamBirthYears
) betweenTeam
andBirthYear
into the database. From the viewpoint of storage space or performance you wouldn't probably win anything (since theBirthYear
class is very small and a record in theBirthYear
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 theBirthYear
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.