C# 使用代码优先的实体框架多对多关系

C# 使用代码优先的实体框架多对多关系,c#,entity-framework,ef-code-first,entity-framework-6,C#,Entity Framework,Ef Code First,Entity Framework 6,我有两门课:Foo和Bar public class Foo { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public virtual int Id { get; set; } private ICollection<Bar> _bar; public virtual ICollection<Bar> Bars {

我有两门课:Foo和Bar

public class Foo
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual int Id { get; set; }


        private ICollection<Bar> _bar;
        public virtual ICollection<Bar> Bars
        {
            get { return _bar?? (_bar= new Collection<Bar>()); }
            set { _bar= value; }
        }
    }

这不是我想要的。我想我在酒吧里加了两套食物,把事情搞砸了。我现在该怎么办

解决了,我需要做两件事来解决这个问题。首先,向Foo添加另一个Bar集合

 public class Foo
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public virtual int Id { get; set; }


            private ICollection<Bar> _bar;
            public virtual ICollection<Bar> Bars
            {
                get { return _bar?? (_bar= new Collection<Bar>()); }
                set { _bar= value; }
            }

            private ICollection<Bar> _bar2;
            public virtual ICollection<Bar> Bars2
            {
                get { return _bar2?? (_bar2= new Collection<Bar>()); }
                set { _bar2= value; }
            }
        }
公共类Foo
{
[数据库生成(DatabaseGeneratedOption.Identity)]
公共虚拟整数Id{get;set;}
私人ICollection酒吧;
公共虚拟ICollection条
{
获取{return}bar???({bar=new Collection());}
设置{u bar=value;}
}
私人ICollection_bar2;
公共虚拟ICollection条2
{
获取{return _bar2??(_bar2=new Collection());}
设置{u bar2=value;}
}
}
第二,使用InverseProperty明确告诉EF我想要2个多对多关系

 public class Bar
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public virtual int Id { get; set; }


            private ICollection<Foo> _foo1;
            [InverseProperty("Bars")]
            public virtual ICollection<Foo> Foos1
            {
                get { return _foo1?? (_foo1= new Collection<Foo>()); }
                set { _foo1= value; }
            }

               private ICollection<Foo> _foo2;
                [InverseProperty("Bars2")]
                public virtual ICollection<Foo> Foos2
                {
                    get { return _foo2?? (_foo2= new Collection<Foo>()); }
                    set { _foo2= value; }
                }


            }
公共类栏
{
[数据库生成(DatabaseGeneratedOption.Identity)]
公共虚拟整数Id{get;set;}
私人ICollection\u foo1;
[反向属性(“条”)]
公共虚拟ICollection Foos1
{
获取{return\u foo1??(\u foo1=new Collection());}
设置{u foo1=value;}
}
私人ICollection2;
[反向属性(“Bars2”)]
公共虚拟ICollection Foos2
{
获取{return}
设置{u foo2=value;}
}
}

我现在以4个表Foo、Bar、FooBarss和FooBar1结束。那么,为什么有2个集合?你想实现什么?因为这是我必须应用的商业规则。假设一个项目有两种税,那么您将购买税(购买该项目时适用的税)和销售税(销售该项目时适用的税)。既然你问了,也许我的模型不太好,有什么建议吗?
 public class Foo
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public virtual int Id { get; set; }


            private ICollection<Bar> _bar;
            public virtual ICollection<Bar> Bars
            {
                get { return _bar?? (_bar= new Collection<Bar>()); }
                set { _bar= value; }
            }

            private ICollection<Bar> _bar2;
            public virtual ICollection<Bar> Bars2
            {
                get { return _bar2?? (_bar2= new Collection<Bar>()); }
                set { _bar2= value; }
            }
        }
 public class Bar
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public virtual int Id { get; set; }


            private ICollection<Foo> _foo1;
            [InverseProperty("Bars")]
            public virtual ICollection<Foo> Foos1
            {
                get { return _foo1?? (_foo1= new Collection<Foo>()); }
                set { _foo1= value; }
            }

               private ICollection<Foo> _foo2;
                [InverseProperty("Bars2")]
                public virtual ICollection<Foo> Foos2
                {
                    get { return _foo2?? (_foo2= new Collection<Foo>()); }
                    set { _foo2= value; }
                }


            }