0

I have a code first EF Model with a simple one(PartDetails)-to-many(Parts) relationship.

While the system is offline the lookup (PartDetails) table is purged and re-populated by external job, violating the constraint.

modelBuilder.Entity<Part>().HasOptional(e => e.PartDetails)
                           .WithMany()
                           .HasForeignKey(k => k.PartDetailsId);

I have tried .Map and other variants but all create the DB constraint.

How can I prevent EF from creating the DB FK constraint using the fluent API?

I understand this may not be the best architectural approach but I'm stuck with it. If I can't find a solution to do this with the Fluent API I will manually drop the constraint in a migration script or drop and re-add the constraint during the purge job.

Masoud
  • 8,020
  • 12
  • 62
  • 123
  • This may help: http://stackoverflow.com/questions/159038/can-foreign-key-constraints-be-temporarily-disabled-using-t-sql – Masoud Sep 25 '14 at 06:33

1 Answers1

0

You could manually drop the constraint in a migration script using Sql("DROP CONSTRAINT...").

But a better architectural approach would be to drop and re-add the constraint during the purge job. It sounds like the purge job is where the problem really lies. Constraints are very important.

Colin
  • 22,328
  • 17
  • 103
  • 197