none
Entity Framework Code-First Required One-To-One Passes Model Validation with missing relation RRS feed

  • Question

  • I have the following two Entities that I have a Required One-to-One Relationship that is configured using the Fluent API.  I am using the MSDN example of Instructor and OfficeAssignment:

    public class Instructor
        {
            // Primary key
            public int InstructorID { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public System.DateTime HireDate { get; set; }

            // Navigation properties
            public virtual OfficeAssignment OfficeAssignment { get; set; }
        }

    public class OfficeAssignment
        {
            // Specifying InstructorID as a primary
            [Key()]
            public Int32 InstructorID { get; set; }

            public string Location { get; set; }

            // Navigation property
            public virtual Instructor Instructor { get; set; }
        }
    }

    My context is configured as:

          // Configure the primary key for the OfficeAssignment
                modelBuilder.Entity<OfficeAssignment>()
                    .HasKey(t => t.InstructorID);

                modelBuilder.Entity<Instructor>()
                    .HasRequired(t => t.OfficeAssignment)
                    .WithRequiredPrincipal(t => t.Instructor);

    SO, my understanding is that I can't insert an Instructor without an associated OfficeAssignment.  An instructor requires an OfficeAssignment.  However, when I issue the following code, Entity Framework does not validate that the model is invalid and allows me to insert an Instructor without and associated OfficeAssignment.   

    Instructor i = new Instructor
                {
                    FirstName = "Sam",
                    LastName = "Smith",
                    HireDate = DateTime.Now,
                    OfficeAssignment = null
                    
                };

                using (AppDbContext context = new AppDbContext())
                {
                    context.Instructors.Add(i);
                    context.SaveChanges();
                }

    My understanding from all of my reading is that EF should raise an validation exception telling me that an Instructor must have an associated OfficeAssignment but it does not and saves the data.  Yes, I have the context configured to perform validation.  

    Monday, June 4, 2012 11:39 PM

Answers

  • Hi NETDEV100,

    Welcome to MSDN Forum.

    The strict 1 to 1 mapping is possible only if both 'Instructor' and 'OfficeAssignment' will be mapped to the same table. Because strict 1:1 means that you cannot insert 'Instructor' without existing 'OfficeAssignment' and you also cannot insert 'OfficeAssignment' without existing 'Instructor' . Each record has its own insert command, database will check the integrity after each command, not after transaction. So, if you want to create a strict 1 to 1 mapping, please try this method. Mapping two entities to one table.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 6, 2012 3:50 AM
    Moderator