none
How do I add and remove entities with a 1-to-1 relationship? RRS feed

  • Question

  • WHAT I HAVE:

    Visual Basic 2015, .NET 4.0, Entity Framework 6

    MY PROBLEM:

    Sometimes when I define an entity type, there are properties that would take up a lot of memory when loaded, so I think of splitting the entity type into 2 types with a 1-to-1 relationship;--that is, an entity instance of Type1 is matched with exactly one entity instance of Type2 (not 0 or multiple) and vice versa. I could then put the properties I use frequently use into an instance of Type1, and put the lesser-used-but-more-memory-using properties into its counterpart instance of Type2.

    The problem is, I haven't seen or used code for which two entities have a mandatory 1-to-1 relationship. When I code an Add or Remove on an entity of Type1, do I have to code an Add or Remove for Type2, and if so, in what order?

    Imagine the following:

    An instance of a Type1 entity, et1,  has navigation property ReferenceToType2, and an instance of Type2, et2, has the nav-prop ReferenceToType1

    ADD:

    et1.ReferenceToType2 = et2  : et2.ReferenceToType1 = et1

    context.Set(Of Type1).Add(et1) 

    >>>Do I also have to have to do context.Set(Of Type2).Add(et2), and should it be before or after the above statement?

    REMOVE:

    et1 = query returning instance of Type1 entity

    context.Set(Of Type1).Remove(et1)

    >>>Do I also have to do context.Set(Of Type2).Remove(et1.ReferenceToType2), and should it be before or after the above statement?




    Robert Gustafson


    Wednesday, March 7, 2018 9:24 PM

Answers

  • This is how to do it:

    ADD:

    et1.ReferenceToType2 = et2  : et2.ReferenceToType1 = et1

    context.Set(Of Type1).Add(et1) 

    REMOVE:

    et1 = query returning instance of Type1 entity

    context.Set(Of Type2).Remove(et1.ReferenceToType2) : context.Set(Of Type1).Remove(et1)


    Robert Gustafson

    Saturday, March 24, 2018 1:24 AM

All replies

  • The problem is, I haven't seen or used code for which two entities have a mandatory 1-to-1 relationship. When I code an Add or Remove on an entity of Type1, do I have to code an Add or Remove for Type2, and if so, in what order?

    One would assume that there is  a physical relationship where there is a foreign-key relationship even on a 1-1 relationship.  In this case, you would have to delete what would be considered the child that has the relationship back to the parent first before you could delete the parent. Why would that be any different than a 1-2-many?

    Wednesday, March 7, 2018 9:53 PM
  • Hi RobertGustafson,

    >>The problem is, I haven't seen or used code for which two entities have a mandatory 1-to-1 relationship. When I code an Add or Remove on an entity of Type1, do I have to code an Add or Remove for Type2, and if so, in what order?

    I think entity framework does not support one-to-one relationship, only support One-to-Zero-or-One Relationship, When we add an entity of Type1, we don't have code add method for Type2, if we config Cascade Delete in Fluent API, we could also don't have to code an remove for Type2, and the following sample for your reference.

    #Model

    public class Student
    {
            public int StudentId { get; set; }
            public string StudentName { get; set; }
            public virtual StudentAddress Address { get; set; }
    }
    
    public class StudentAddress
    {
            public int StudentAddressId { get; set; }
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string City { get; set; }
            public int Zipcode { get; set; }
            public string State { get; set; }
            public string Country { get; set; }
            public virtual Student Student { get; set; }
    }

    #DbContext

    public partial class Model1 : DbContext
        {
            public Model1()
                : base("name=Model1")
            {
            }
    
            public DbSet<Student> Student { get; set; }
            public DbSet<StudentAddress> StudentAddress { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                // Configure StudentId as FK for StudentAddress
                modelBuilder.Entity<Student>()
                            .HasRequired(s => s.Address)
                            .WithRequiredPrincipal(ad => ad.Student)
                            .WillCascadeOnDelete(true);
            }
        }

    #Add

    using (var db = new Model1())
    {
       Student student = new Student() { StudentName = "Cole" };
       StudentAddress address = new StudentAddress() { Address1 = "Address1", Address2 = "Address2", City = "City", Country = "Country", State = "State", Zipcode = 100000};
       student.Address = address;
       db.Student.Add(student);
       db.SaveChanges();
    }

    #Remove

    using (var db = new Model1())
    {

    Student student = db.Student.Find(1); db.Student.Remove(student); db.SaveChanges();

    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 8, 2018 6:49 AM
    Moderator
  • >>The problem is, I haven't seen or used code for which two entities have a mandatory 1-to-1 relationship. When I code an Add or Remove on an entity of Type1, do I have to code an Add or Remove for Type2, and if so, in what order?

    I think entity framework does not support one-to-one relationship, only support One-to-Zero-or-One Relationship, When we add an entity of Type1, we don't have code add method for Type2, if we config Cascade Delete in Fluent API, we could also don't have to code an remove for Type2, and the following sample for your reference.

    -------------------------------------------------------------------------------

    You can do this with EF. CRUD operations would need to be done manually with EF by the developer treating each entity separately in a stand alone manner. You insert the Type1, get the ID back and apply the Type1 id to the Type2 Foreign-key property.  I would consider it a logical or pseudo relationship between type1 and type2. 

    Thursday, March 8, 2018 10:09 AM
  • This is how to do it:

    ADD:

    et1.ReferenceToType2 = et2  : et2.ReferenceToType1 = et1

    context.Set(Of Type1).Add(et1) 

    REMOVE:

    et1 = query returning instance of Type1 entity

    context.Set(Of Type2).Remove(et1.ReferenceToType2) : context.Set(Of Type1).Remove(et1)


    Robert Gustafson

    Saturday, March 24, 2018 1:24 AM