实体框架允许多对多关系中的重复项 [英] Entity framework allow duplicates in many-to-many relation
问题描述
我有2个对象-多对多关系中的父母和孩子,问题是1个父母可以有2个相同的孩子,但是EF仅将此关系保存1次。
I have 2 object - parents and childs in relationship many-to-many, the issue is that 1 parent can have 2 the same childs, but EF saves this relationship only 1 time.
我只找到2个有效的解决方案:
I found only 2 working solutions:
- 添加
count
列到表中并手动填充 - 不使用多对多,而是将其拆分为一对多和多对一
,但我不喜欢这个解决方案,因为我希望可以有一些更简单的解决方案。
- add
count
column into table and manually fill it - not use many-to-many, but split it into one-to-many and many-to-one but I do not like this solutions, because I hope that there could be some more simple solution.
您能帮我吗?
编辑:
联结表示例:
Example of junction table:
1-1
1-1
1-2
1-3
2-3
代码:
模型
public class Item
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public virtual ICollection<Item> childs { get; set; }
public virtual ICollection<Item> parents { get; set; }
}
数据库上下文
modelBuilder.Entity<Item>().
HasMany(i => i.childs).
WithMany(i2 => i2.parents).
Map(
m =>
{
m.MapLeftKey("parentId");
m.MapRightKey("childId");
m.ToTable("itemRelationship");
});
推荐答案
关系数据库是关于集合的>。集合是已识别实体的不同集合。但是,接线表的拍摄方式是袋子。这违反了关系理论的基础。像往常一样,一次违规会引发其他违规。下一个是没有唯一主键,即身份。
Relational databases are about sets. A set is a distinct collection of identified entities. Your junction table however, the way you're picturing it, is a bag. This violates the fundamentals of relational theory. As usual, one violation gives rise to other violations. Not having a unique primary key, an identity, is the next one. Not being able to refer to these junction records by foreign keys, should you want that, is another one.
所以只是不要这么做。
b
$ b
我认为这里的错误是您要表达以下事实: A
具有 n $ c
B
的$ c>实例,取决于您创建的行数。
I think the mistake here is that you want to express the fact "A
has n
instances of B
" by the number of rows you create. But this number is an attribute of the association.
让我们看一个示例:文章和单词。
Let's look at an example: Articles and Words.
您可以通过纯粹的多对多关联来表达文章
和 Word
之间的关系。该关联表示: this Article
包含 these Word
s 。观看指示代词(此,这些)。他们暗示身份。数据库中每个文章都会有一个实例,每个单词都会有一个实例。
You could express the relation between Article
and Word
by a pure many-to-many association. This association expresses: this Article
contains these Word
s. Watch the demonstrative pronouns (this, these). They imply identity. There will be one instance of each article and one instance of each word in the database.
如果要存储单词出现的次数,则必须添加此数字作为关联的属性。事实永远不会由多行建模。单行描述了以下事实:此 Article
出现了 n
个单词$ c> c $ c>。在Entity Framework中,这意味着关联成为类模型中的类,例如 ArticleWord
。该协会的多重性是 Article
1-n ArticleWord
n-1 Word
。
If you want to store the number of times a word occurs, you have to add this number as an attribute to the association. A fact is never modelled by a number of rows. The single row describes the fact "this Article
has n
occurrences of that Word
". In Entity Framework this means that the association becomes a class in the class model, for instance ArticleWord
. The multiplicities of the association are Article
1-n ArticleWord
n-1 Word
.
这篇关于实体框架允许多对多关系中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!