zoukankan      html  css  js  c++  java
  • EF框架一对多 多对多关系总结

    原文连接 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">&lt;</span>npc<span class="token punctuation">&gt;</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">&lt;</span>npc<span class="token punctuation">&gt;</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">&gt;</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">&gt;</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">&gt;</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">&gt;</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">&gt;</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">&lt;</span>npc<span class="token punctuation">&gt;</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">&lt;</span>npc<span class="token punctuation">&gt;</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">&gt;</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">&gt;</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">&gt;</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">&gt;</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">&gt;</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">&gt;</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>
  • 相关阅读:
    C++里面关于虚函数的一些注意点
    HDOJ 2736 Surprising Strings
    hdu
    hdu 4404 Worms(多边形与圆的交)
    HTC one/M7电信802d 毒蛇ViperOne2.1.0/高级毒蛇工具/完美root,精简/更多自定义,稳定,流畅ROM
    Replace
    Centos环境下部署游戏服务器-权限
    jdk1.6与1.7垃圾回收
    java--基于socket的网络传输开发
    番外:android模拟器连不上网
  • 原文地址:https://www.cnblogs.com/sunny3158/p/11800948.html
Copyright © 2011-2022 走看看