i am using Entity Framework with code first approach.
In my onModelCreating
I am creating my tables with keys and relationships (I am using Fluent API approach, not data annotations).
But when I try to generate my model using Update-Database
command I receive following error
Introducing FOREIGN KEY constraint 'FK_customers.invoices_customers.billingCenters_billingCenterId' on table 'invoices' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.
I almost sure I do not have cycles... and I would not have problem if I had cascade path. It's what I would like to have instead!
Following the model I am creating:
modelBuilder.Entity<Customer>()
.ToTable("customers", schemaName)
.HasKey(c => new { c.Code });
modelBuilder.Entity<BillingCenter>()
.ToTable("billingCenters", schemaName)
.HasKey(bc => new { bc.Id });
//1 Customer -> N BillingCenters
modelBuilder.Entity<BillingCenter>()
.HasRequired(bc => bc.Customer)
.WithMany(c => c.BillingCenters)
.HasForeignKey(bc => bc.CustomerId);
modelBuilder.Entity<Invoice>()
.ToTable("invoices", schemaName)
.HasKey(i => new { i.Id });
//Here the code gives me problems
//1 BillingCenter -> N Invoices
modelBuilder.Entity<Invoice>()
.HasRequired(i => i.BillingCenter)
.WithMany(bc => bc.Invoices)
.HasForeignKey(i => i.BillingCenterId);
modelBuilder.Entity<Payment>()
.ToTable("payments", schemaName)
.HasKey(ep => new { ep.Id })
.Property(ep => ep.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//1 Customer -> N Payments
modelBuilder.Entity<Payment>()
.HasRequired(ep => ep.customer)
.WithMany(c => c.Payments)
.HasForeignKey(ep => ep.customerCode);
//1 Invoice -> N Payments (Failed, Ok, ...)
modelBuilder.Entity<Payment>()
.HasRequired(p => p.Invoice)
.WithMany(i => i.Payments)
.HasForeignKey(p => p.InvoiceId);
If I remove this code everything seems to work
modelBuilder.Entity<Invoice>()
.HasRequired(i => i.BillingCenter)
.WithMany(bc => bc.Invoices)
.HasForeignKey(i => i.BillingCenterId);
And the following database is generated:
I say that it SEEMS to work because if I see the relation between billingCenters
and invoices
the delete rule
is no action
.
How Can I solve the problem?
Thank you in advance