When using the Entity Framework Code First 4.3.1, you can create relationships with a plurality of 1 to 1. That is, one entity at each end of the relationship.
You can configure the 1-to-1 relationship required-required or required-optional ^. However, when I switch between them, I see no differences in:
- Database schema created. I am targeting SQL Server 2008.
- EF runtime
That way, I can create a RequiredPrincipalAs entry without the corresponding RequiredDependentAs entry, even though the relationship has been configured as required . This seems to contradict the documentation for HasRequired (...) :
Configures the required association with this type of object. Entity type instances cannot be stored in the database unless this relationship is specified. The foreign key in the database will be invalid.
http://msdn.microsoft.com/en-us/library/gg671317
required relationship:
public class RequiredPrincipalA { public int Id { get; set; } public virtual RequiredDependentA DependentA { get; set; } } public class RequiredDependentA { public int Id { get; set; } public virtual RequiredPrincipalA PrincipalA { get; set; } }
Objects of the relationship are required-optional :
public class RequiredPrincipalB { public int Id { get; set; } public virtual OptionalDependentB DependentB { get; set; } } public class OptionalDependentB { public int Id { get; set; } public virtual RequiredPrincipalB PrincipalB { get; set; } }
DbContext configuration and models:
public class AppContext : DbContext { public DbSet<RequiredPrincipalA> PrincipalAs { get; set; } public DbSet<RequiredDependentA> DependentAs { get; set; } public DbSet<RequiredPrincipalB> PrincipalBs { get; set; } public DbSet<OptionalDependentB> DependentBs { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<RequiredPrincipalA>() .HasRequired(o => o.DependentA) .WithRequiredPrincipal(o => o.PrincipalA); modelBuilder.Entity<RequiredPrincipalB>() .HasOptional(o => o.DependentB) .WithRequired(o => o.PrincipalB); } }
Security Code:
Database.SetInitializer(new DropCreateDatabaseAlways<AppContext>()); using (var ctx = new AppContext()) { ctx.Database.Initialize(force: false); ctx.PrincipalAs.Add(new RequiredPrincipalA()); ctx.PrincipalBs.Add(new RequiredPrincipalB()); ctx.SaveChanges(); }
I know that I could add the [Required] data attribute for the RequiredPrincipalA.DependentA and RequiredDependentA.PrincipalA navigation properties. This will cause EF checking to prevent the above scenario. However, I do not want to do this because it also checks that the navigation property is populated when updating an existing object. This means that the application must first obtain an object at the other end of the relationship for each update.
Why can't I see the difference in EF behavior only when the relationship between mandatory and mandatory-optional changes?
^ Please note that optional-optional is also supported, but this is not part of my question. There are obvious differences in the generated database schema and runtime when an optional-optional relationship is configured.