Entity Framework (EF) Element Code First cascaded output for a one to zero or one relationship

Following the section “First Code Modeling” in the Pluralsight course “Getting Started with Entity Framework 5” by Julie Lerman , I created two POCO classes with a one-to-zero ratio or : parent (User) and additional child (UserDetail).

Chart of user data model and UserDetail (click to view).

Notice in the diagram that the UserId property is the primary key and foreign key for the UserDetail .

Relevant Code:

public class User { //... [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } /* Has a 1:0..1 relationship with UserDetail */ public virtual UserDetail UserDetail { get; set; } //... } public class UserDetail { //... /* Has a 0..1:1 relationship with User */ public virtual User User { get; set; } [Key, ForeignKey("User")] public int UserId { get; set; } //... } public class EFDbContext : DbContext { public DbSet<User> Users { get; set; } //public DbSet<UserDetail> UserDetails { get; set; } /* Explicit declaration not necessary. Context is aware of UserDetail entity due to 0..1:1 relationship with User */ public EFDbContext() { Configuration.ProxyCreationEnabled = true; Configuration.LazyLoadingEnabled = true; } } public class UserRepository : IUserRepository { private EFDbContext _context = new EFDbContext(); public void Delete(User entity) { entity = _context.Users.Find(entity.UserId); //... _context.Users.Remove(entity); _context.SaveChanges(); //... } } 

When the Delete () method in the UserRepository class is called, it does not delete the user record in the database because the foreign key in UserDetail does not have cascading deletion enabled.

DELETE operation was contrary to the REFERENCE constraint "FK_dbo.UserDetail_dbo.User_UserId".

How do I enable cascading deletions for a one-to-zero or one relationship using Entity Framework First code (to remove a user, I automatically deleted UserDetail)?

+50
c # entity-framework-5 entity-framework ef-code-first cascading-deletes
Jul 05 '13 at 11:13
source share
3 answers

To do this, you will need to use the free API.

Try adding the following to your DbContext :

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<User>() .HasOptional(a => a.UserDetail) .WithOptionalDependent() .WillCascadeOnDelete(true); } 
+67
Jul 05 '13 at 11:34 on
source share

This code worked for me

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<UserDetail>() .HasRequired(d => d.User) .WithOptional(u => u.UserDetail) .WillCascadeOnDelete(true); } 

Migration code:

 public override void Up() { AddForeignKey("UserDetail", "UserId", "User", "UserId", cascadeDelete: true); } 

And everything turned out fine. When I first used

 modelBuilder.Entity<User>() .HasOptional(a => a.UserDetail) .WithOptionalDependent() .WillCascadeOnDelete(true); 

Migration code:

 AddForeignKey("User", "UserDetail_UserId", "UserDetail", "UserId", cascadeDelete: true); 

but it does not match either of the two available overloads (in EntityFramework 6)

+1
Nov 09 '16 at 0:29
source share

You can also disable the cascade exclusion agreement in the global scope of your application by following these steps:

 modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>() modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>() 
0
Jul 11 '18 at 2:03
source share



All Articles