1

I have 2 classes Foo and 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; }
        }
    }

Bar

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


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

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


            }

I then use Migration to update the database. However, instead of having an usual table FooBar. EF creates 2 tables

Foo
Id,Bar_Id

Bar
Id,Foo_Id,Foo_Id1

which is not what I want. I guess I'm messing up by adding 2 collections of Foo into Bar. What should I do now?

2
  • Well, why do you have 2 collections? What are you trying to achieve? Commented Apr 23, 2015 at 10:13
  • Because that's the business rules that I have to apply. Think an item with 2 kind of taxes, so you will have bought taxes (taxes applied when you bought the item) and sold taxes (taxes will be applied when you sell the item). Now that you ask, maybe my model is not that good, any suggestion? Commented Apr 23, 2015 at 10:17

1 Answer 1

1

Figured it out, I need to do 2 things to fix this. First, add another collection of Bar to Foo

 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; }
            }
        }

Second, use InverseProperty to explicitly tell EF that I want 2 many-to-many relationships.

 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; }
                }


            }

I now end up with 4 table Foo,Bar,FooBarss and FooBar1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.