我可以先使用Entity Framework代码为单个Collection属性创建单独的表吗? [英] Can I create a separate table for a single Collection property using Entity Framework code first?
问题描述
我有一个 Foo
与 ICollection< string>
。为了能够通过实体框架存储这个,我创建了一个 Bar
与 Id
和 Value
属性。 Entity Framework创建一个 Bar
的表,其外键列为 Foo
。
问题是我有一大堆的类有这样 ICollection< string>
。我将它们全部转换为 ICollection< Bar>
。然而,结果是 Foo
表现在包含具有 ICollection< Bar>
的每个类的外键列, (10 +)。
虽然我知道有必要这样做以使这些关系在数据库中工作,但是数据库图表不会更直观。
我在想,就像在多对多的关系中,我想在每个关系之间有一张表。例如,
- 一个表
Foo_Bar
,有两列:Foo
和Bar之间的关系的> Foo_Id
和Bar_Id
和另一个表Baz_Bar
与Baz_Id
和Bar_Id
。
为了实现这一点,我可以创建 Bar
(例如 FooBar
);更改集合以使用那些(例如 ICollection< FooBar>
);并让EF使用每个类型的表创建数据库(例如通过 [Table(Foo_Bar)]
FooBar >)方法。但我想防止必须创建所有这些空类。
我想知道是否可能以某种方式指定这个行为只是基于属性。类似的东西:
public class Foo
{
[Table(Foo_Bar)]
public ICollection< Bar>酒吧{get;组; }
}
注意:如果这种方法在第一个请
总结评论:
你想要什么(即:5个表)是Entity Framework的一个多对多的关系。
按照惯例,你通过设置一个集合
Foo {
ICollection< Bar& bars
}
Foo {
ICollection< Foo> Foos
}
如果你不想反向集合通过流利的API设置关系。
modelBuilder.Entity< Foo> .HasMany(x => x.Bars) .WithMany()
AFAIK没有注释允许使用注释达到最后的结果。 / p>
然后你必须手工处理Foo的一个吧。解决方案可以是由EF创建的连接表的条形键的唯一约束。
I have a class Foo
with an ICollection<string>
. To be able to store this through Entity Framework, I have created a class Bar
with an Id
and Value
property. Entity Framework creates a table for Bar
with a foreign key column to Foo
. This works well.
The problem is that I have a whole bunch of classes with such a ICollection<string>
. I converted them all to ICollection<Bar>
. The result however is that the Foo
table now contains a foreign key column for every class that has an ICollection<Bar>
(10+).
While I understand that it's necessary to do this to have those relationships work in the database, the database diagram doesn't get more intuitive this way.
I was thinking that, like in a many to many relationship, I would like to have a table in between each of those relationships. For example,
- a table
Foo_Bar
with two columns:Foo_Id
andBar_Id
for the relationship betweenFoo
andBar
and another tableBaz_Bar
withBaz_Id
andBar_Id
.
To achieve this, I can create a sub class of Bar
(e.g. FooBar
) for every relationship; change the collection to use those (e.g. ICollection<FooBar>
); and let EF create the database using the table per type (e.g. by decorating FooBar
with [Table("Foo_Bar")]
) approach for these types. But I would like to prevent having to create all these empty classes.
I wonder if it is possible to somehow specify this behaviour just based on the property. Something like this:
public class Foo
{
[Table("Foo_Bar")]
public ICollection<Bar> Bars { get; set; }
}
On a side note: If this approach is a bad idea in the first place, don't hesitate to let me know :).
to summarize comments:
what you want (i.e.: 5 tables) is a many to many relation for Entity Framework.
By conventions you get it by setting a collection on both entities
Foo {
ICollection<Bar> Bars
}
Foo {
ICollection<Foo> Foos
}
if you don't want the reverse collections (Foos) then you have to setup the relation by the fluent API.
modelBuilder.Entity<Foo>.HasMany(x => x.Bars).WithMany()
AFAIK there is no annotation allowing to reach this last result by using the Annotations.
You then have to handle by hand the unicity of Foo for a Bar. A solution may be a unique constraint on the Bar Key of the join table created by EF.
这篇关于我可以先使用Entity Framework代码为单个Collection属性创建单独的表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!