0

The DELETE statement conflicted with the REFERENCE constraint > "FK_dbo.VendorDocuments_dbo.VendorModels_VendorId". The conflict occurred in database > "aegisv", table "dbo.VendorDocuments", column 'VendorId'. The statement has been terminated.

Means I have one table of VendorModel which has list of invoicedocument(it is a model with foreign key vendorId of table vendor model). when I am trying to delete Vendor model from database I am getting above exception.

I have tried various things but I haven't resolve same yet.

Have added following code but doesn't worked.

      modelBuilder.Entity<VendorDocument>()
            .HasRequired(x => x.VendorModel)
            .WithMany(u => u.VendorDocumemts)
            .WillCascadeOnDelete();

Please any one can help me.. Thank you.

Szymon
  • 42,577
  • 16
  • 96
  • 114
Vikas
  • 127
  • 10

2 Answers2

0

Just change in little bit code here,try it :-

 modelBuilder.Entity<VendorDocument>()
            .HasOptional(j => j.VendorModel)
            .WithMany(u => u.VendorDocumemts)
            .WillCascadeOnDelete(true);
kaushik0033
  • 679
  • 6
  • 12
  • Dude have tried same and now I am getting following exception during login..Multiplicity conflicts with the referential constraint in Role 'VendorDocument_VendorModel_Target' in – Vikas Oct 08 '13 at 11:34
  • Please see link http://stackoverflow.com/questions/8007129/codefirst-ef4-1-mvc-against-legacy-database-multiplicity-conflicts – kaushik0033 Oct 08 '13 at 12:37
0

You have to enable cascade delete rule on database also. EF does not take responsibility for cascading the delete in the database.

The EF is responsible for the correctness of the ObjectContext after SaveChanges(). So the EF attempts to synchronize the ObjectContext, with the expected database state after the expected cascade in the database.

So If you have created Cascade Delete rule in your model, you must have to configure Cascade delete rule in your database. Tip 33

To enable Cascade Delete rule in database -Enable CascadeDelete

Community
  • 1
  • 1
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36