Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jun 16, 2012 08:16 AM by r.rajeshkhunt
Member
7 Points
69 Posts
Jun 16, 2012 08:05 AM|LINK
Hi
I am developing a app in asp.net mvc3 using code first scenario,
Product is parent and order is child
i want to delete product table record when child table also have the data.
I think it possible with the cascade rule,but I dont know how to use it in code first scenario
can anybody help me in this
Participant
1358 Points
250 Posts
Jun 16, 2012 08:16 AM|LINK
The following fluent API code perfectly switch on the cascade delete on the database:
public class Student { public virtual int StudentId { get; set; } public virtual Anamnesis Anamnesis { get; set; } } public class Anamnesis { public int AnamnesisId { get; set; } public virtual Student Student { get; set; } } public class Context : DbContext { public DbSet<Student> Students { get; set; } public DbSet<Anamnesis> Anamnesises { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Student>() .HasRequired(s => s.Anamnesis) .WithRequiredPrincipal(a => a.Student) .WillCascadeOnDelete(); } }
chandana G
Member
7 Points
69 Posts
How to use the cascade rule using code first in asp.net mvc3
Jun 16, 2012 08:05 AM|LINK
Hi
I am developing a app in asp.net mvc3 using code first scenario,
Product is parent and order is child
i want to delete product table record when child table also have the data.
I think it possible with the cascade rule,but I dont know how to use it in code first scenario
can anybody help me in this
r.rajeshkhun...
Participant
1358 Points
250 Posts
Re: How to use the cascade rule using code first in asp.net mvc3
Jun 16, 2012 08:16 AM|LINK
The following fluent API code perfectly switch on the cascade delete on the database:
public class Student { public virtual int StudentId { get; set; } public virtual Anamnesis Anamnesis { get; set; } } public class Anamnesis { public int AnamnesisId { get; set; } public virtual Student Student { get; set; } } public class Context : DbContext { public DbSet<Student> Students { get; set; } public DbSet<Anamnesis> Anamnesises { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Student>() .HasRequired(s => s.Anamnesis) .WithRequiredPrincipal(a => a.Student) .WillCascadeOnDelete(); } }======================================
Microsoft® Community Contributor 2011 | AWARD
MCTS
My Blog
Follow Me