原文连接 https://blog.csdn.net/hhhhhhenrik/article/details/89216909
总结几种项目中遇到的一对多 多对多关系
1 基本的多对多关系
public class npc
{
public string Name;
public ICollection<Content> Contents;
}
public class Content
{
public string Name;
<span class="token keyword">public</span> ICollection<span class="token generics function"><span class="token punctuation"><</span>npc<span class="token punctuation">></span></span> npcs<span class="token punctuation">;</span>
}
//fluent api
HasMany(m => m.Contents).WithMany(w => w.npcs).Map(m =>
{
m.ToTable("C_NPC_Content_Relation");
m.MapLeftKey("npcId");
m.MapRightKey("ContentId");
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2 同一个表建立两个多对多关系
public class npc
{
public string Name;
public ICollection<Content> Contents1;
public ICollection<Content> Contents2;
}
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Content</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> string Name<span class="token punctuation">;</span>
<span class="token keyword">public</span> ICollection<span class="token generics function"><span class="token punctuation"><</span>npc<span class="token punctuation">></span></span> npcs<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//fluent api</span>
<span class="token function">HasMany</span><span class="token punctuation">(</span>m <span class="token operator">=</span><span class="token operator">></span> m<span class="token punctuation">.</span>Contents1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">WithMany</span><span class="token punctuation">(</span>w <span class="token operator">=</span><span class="token operator">></span> w<span class="token punctuation">.</span>npcs<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Map</span><span class="token punctuation">(</span>m <span class="token operator">=</span><span class="token operator">></span>
<span class="token punctuation">{</span>
m<span class="token punctuation">.</span><span class="token function">ToTable</span><span class="token punctuation">(</span><span class="token string">"C_NPC_Content1_Relation"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">MapLeftKey</span><span class="token punctuation">(</span><span class="token string">"npcId"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">MapRightKey</span><span class="token punctuation">(</span><span class="token string">"ContentId"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">HasMany</span><span class="token punctuation">(</span>m <span class="token operator">=</span><span class="token operator">></span> m<span class="token punctuation">.</span>Contents2<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">WithMany</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Map</span><span class="token punctuation">(</span>m <span class="token operator">=</span><span class="token operator">></span>
<span class="token punctuation">{</span>
m<span class="token punctuation">.</span><span class="token function">ToTable</span><span class="token punctuation">(</span><span class="token string">"C_NPC_Content2_Relation"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">MapLeftKey</span><span class="token punctuation">(</span><span class="token string">"npcId"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
m<span class="token punctuation">.</span><span class="token function">MapRightKey</span><span class="token punctuation">(</span><span class="token string">"ContentId"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
3 普通的一对多
public class npc
{
public string Name;
public int ContentId;//外键
}
public class Content
{
public string Name;
public ICollection<npc> npcs;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
4 同一个表建立两份一对多关系
public class npc
{
public string Name;
public int ContentId_1;//外键1
public int ContentId_2;//外键2
}
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Content</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> string Name<span class="token punctuation">;</span>
<span class="token keyword">public</span> ICollection<span class="token generics function"><span class="token punctuation"><</span>npc<span class="token punctuation">></span></span> npcs_1<span class="token punctuation">;</span>
<span class="token keyword">public</span> ICollection<span class="token generics function"><span class="token punctuation"><</span>npc<span class="token punctuation">></span></span> npcs_2<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//fluent api(npc)</span>
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">HasRequired</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">></span> t<span class="token punctuation">.</span>Content<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">WithMany</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">></span> t<span class="token punctuation">.</span>npcs_1<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">HasForeignKey</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">></span> t<span class="token punctuation">.</span>ContentId_1<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">WillCascadeOnDelete</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">HasOptional</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">></span> t<span class="token punctuation">.</span>Content<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">WithMany</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">></span> t<span class="token punctuation">.</span>npcs_2<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">HasForeignKey</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">></span> t<span class="token punctuation">.</span>ContentId_2<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">WillCascadeOnDelete</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
</div>