I have two classes that relate to one another (one-to-many) and I thought I had the properties setup correctly, but when I run the Update-Database command for my migration, I get the following error:
Introducing FOREIGN KEY constraint
'FK_dbo.ParentEnrollment_dbo.CellGroup_CellGroupID' on table
'ParentEnrollment' 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 or index. See previous
errors.
My two classes:
[Table("CellGroup")]
public class CellGroup : BaseEntity
{
public Guid AcademicYearID { get; set; }
[ForeignKey("AcademicYearID")]
public virtual AcademicYear AcademicYear { get; set; }
public Guid LeaderID { get; set; }
[ForeignKey("LeaderID")]
public virtual Parent Leader { get; set; }
public Guid PreviousGroupID { get; set; }
[ForeignKey("PreviousGroupID")]
public virtual CellGroup PreviousGroup { get; set; }
public string Name { get; set; }
public int MaximumSize { get; set; }
public virtual ICollection<ParentEnrollment> Parents { get; set; }
}
and
[Table("ParentEnrollment")]
public class ParentEnrollment : BaseEntity
{
public Guid ParentID { get; set; }
[ForeignKey("ParentID")]
public virtual Parent Parent { get; set; }
public Guid AcademicYearID { get; set; }
[ForeignKey("AcademicYearID")]
public virtual AcademicYear AcademicYear { get; set; }
public bool FirstTimeEnrolling { get; set; }
public string HSLDAAccountNumber { get; set; }
public DateTime HSLDARenewalDate { get; set; }
public string CurrentChurch { get; set; }
public string CurrentChurchContact { get; set; }
public string CurrentChurchPhone { get; set; }
public Guid CellGroupID { get; set; }
[Required]
[ForeignKey("CellGroupID")]
public virtual CellGroup CellGroup { get; set; }
public bool VolunteerBuyOut { get; set; }
public Guid VolunteerPositionID { get; set; }
[ForeignKey("VolunteerPositionID")]
public virtual VolunteerPosition VolunteerPosition { get; set; }
public string VolunteerPositionNotes { get; set; }
public virtual ICollection<StudentEnrollment> StudentEnrollments { get; set; }
}
I only have the Parents property on the CellGroup class so I can easily access the list of enrollments in that cell group. I tried to remove the property to see if it cleared up the warning/error, but it did not. Can someone spot where I have gone wrong with my model(s)?
This error says that you cannot introduce a foreign key from table ParentEnrollment to table CellGroup that has cascading delete enabled, because this will create multiple cascade paths, which is not allowed on SQL Server.
According to the code you posted both tables have relations to a table Parent as well as AcademicYear, which are on non nullable FK columns, so EF will enable cascading on delete by default. With another FK from ParentEnrollment to CellGroup there would be multiple cascade paths, e.g. Parent to CellGroup to ParentEnrollment and Parent to ParentEnrollment, and this is causing your error. Removing the Parent property won't solve this because there still is the same cascading path problem starting from table AcademicYear.
So you have to disable cascading delete for your foreign key, which has to be done using Fluent API in your DbContext like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ParentEnrollment>()
.HasRequired(m => m.CellGroup)
.WithMany(m => m.Parents)
.HasForeignKey(m => m.CellGroupID)
.WillCascadeOnDelete(false);
}
Related
This question already has answers here:
EF7 RC1 : Disable Cascade Delete
(2 answers)
Closed 7 years ago.
In Entity Framework 7 when I am trying to apply a migration I get the error
Introducing FOREIGN KEY constraint 'FK_ChangeOrder_User_CreatedByID' on table 'ChangeOrder' 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 know in older versions of Entity Framework you would deal with this by adding
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
to the DbContext but in EF7 modelBuilder does not seem to have a .Conventions to it and google is only returning older EF 4 though EF 6 results.
How do I specific the ON DELETE NO ACTION constraint in Entity Framework 7?
Edit:
The answer provided by Oleg will apparently do it per Foreign Key but I would like to do it globally as it will much easier to use one line of code to declare this globally then have to specify code it out for every single one of the hundreds of relationships I will end up having.
Edit 2: Code for Oleg
public class ChangeOrder
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public Int16? ApprovedByID { get; set; }
public Byte ApprovalStatusID { get; set; }
public Int16 AssignedToID { get; set; }
public Int16 CreatedByID { get; set; }
public Byte CurrentStatusID { get; set; }
public DateTime? DateApproved { get; set; }
public DateTime? EndDate { get; set; }
public Byte ImpactID { get; set; }
public Byte PriorityID { get; set; }
public DateTime? StartDate { get; set; }
public Byte TypeID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string ReasonForChange { get; set; }
[ForeignKey("ApprovedByID")]
public User ApprovedBy { get; set; }
[ForeignKey("ApprovalStatusID")]
public ChangeApprovalStatus ApprovalStatus { get; set; }
[ForeignKey("AssignedToID")]
public User AssignedTo { get; set; }
[ForeignKey("CreatedByID")]
public User CreatedBy { get; set; }
[ForeignKey("ImpactID")]
public ChangeImpact Impact { get; set; }
[ForeignKey("PriorityID")]
public ChangePriority Priority { get; set; }
[ForeignKey("TypeID")]
public ChangeType ChangeType { get; set; }
[ForeignKey("CurrentStatusID")]
public ChangeStatus CurrentStatus { get; set; }
}
public class JobSightDBContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
base.OnModelCreating(modelbuilder);
}
DbSet<ChangeApprovalStatus> ChangeApprovalStatus { get; set; }
DbSet<ChangeImpact> ChangeImapct { get; set; }
DbSet<ChangeOrder> ChangeOrders { get; set; }
DbSet<ChangePriority> ChangePriorities { get; set; }
DbSet<ChangeStatus> ChangeStatus { get; set; }
DbSet<ChangeType> ChangeTypes { get; set; }
DbSet<User> Users { get; set; }
}
After digging around on GitHub, and working with a very patient guy from MS there, the current solution is to add this to the DbContext
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
foreach (var relationship in modelbuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelbuilder);
}
The construction
modelBuilder.Entity("myNamespace.Models.ChangeOrder", b =>
{
b.HasOne("myNamespace.Models.User")
.WithMany()
.HasForeignKey("CreatedByID")
.OnDelete(DeleteBehavior.Cascade);
});
will means creating FK_ChangeOrder_User_CreatedByID with REFERENCES [dbo].[User] ([CreatedByID]) ON DELETE CASCADE. It should exist in protected override void BuildModel(ModelBuilder modelBuilder) of YourContextModelSnapshot.cs created during migration. I'm not sure that I full understand your question, but I think that you should either add such construct to XXXModelSnapshot.cs or to remove unneeded construct, which already exist here.
UPDATED: I see that you have the problem in the Model. You have the following properties in
public Int16? ApprovedByID { get; set; }
public Int16 AssignedToID { get; set; }
public Int16 CreatedByID { get; set; }
// navigation properties
[ForeignKey("ApprovedByID")]
public User ApprovedBy { get; set; }
[ForeignKey("AssignedToID")]
public User AssignedTo { get; set; }
[ForeignKey("CreatedByID")]
public User CreatedBy { get; set; }
By default migration try to set DeleteBehavior.Cascade on all the properties.
You can overwrite the behavior by changing OnModelCreating, which sets either DeleteBehavior.Restrict behavior for all the keys or to set on one only key the DeleteBehavior.Cascade or DeleteBehavior.SetNull behavior. For example, the below code uses DeleteBehavior.Cascade on CreatedByID (which creates ON DELETE CASCADE on the foreign keys) and DeleteBehavior.Restrict on other foreign keys (no ON DELETE on the foreign keys):
public class JobSightDBContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelbuilder)
{
base.OnModelCreating(modelbuilder);
modelbuilder.Entity(typeof (ChangeOrder))
.HasOne(typeof (User), "ApprovedBy")
.WithMany()
.HasForeignKey("ApprovedByID")
.OnDelete(DeleteBehavior.Restrict); // no ON DELETE
modelbuilder.Entity(typeof (ChangeOrder))
.HasOne(typeof (User), "AssignedTo")
.WithMany()
.HasForeignKey("AssignedToID")
.OnDelete(DeleteBehavior.Restrict); // no ON DELETE
modelbuilder.Entity(typeof (ChangeOrder))
.HasOne(typeof (User), "CreatedBy")
.WithMany()
.HasForeignKey("CreatedByID")
.OnDelete(DeleteBehavior.Cascade); // set ON DELETE CASCADE
}
DbSet<ChangeApprovalStatus> ChangeApprovalStatus { get; set; }
DbSet<ChangeImpact> ChangeImapct { get; set; }
DbSet<ChangeOrder> ChangeOrders { get; set; }
DbSet<ChangePriority> ChangePriorities { get; set; }
DbSet<ChangeStatus> ChangeStatus { get; set; }
DbSet<ChangeType> ChangeTypes { get; set; }
DbSet<User> Users { get; set; }
}
I'm trying to create a linked table that will allow me to have a many to many relationship between my product and accessory tables.
My classes are like this:
public class Product {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Accessory> Accessories { get; set; }
}
public class Accessory {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Machine> Machine { get; set; }
}
public class Type {
public int Id { get; set; }
public string Name { get; set; }
}
The same accessory can be on a product more than once if it is a different type, which will be determined in the link table. Something like this:
public class ProductAccessoryLink {
public int productId {get; set;}
public int accessoryId {get; set;}
public int typeId {get; set}
public int sort {get; set;}
public string notes {get; set}
}
Is this the right approach.
EDIT
This is the error I'm getting when I run update-database:
Introducing FOREIGN KEY constraint
'FK_dbo.ProductAccessoryLinks_dbo.Types_TypeId' on table
'ProductAccessoryLinks' 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.
This is the sql causing the error: ALTER TABLE
[dbo].[ProductAccessoryLinks] ADD CONSTRAINT
[FK_dbo.ProductAccessoryLinks_dbo.Types_TypeId] FOREIGN KEY ([TypeId])
REFERENCES [dbo].[Types] ([Id]) ON DELETE CASCADE
In your case you need to map explicitly the junction table. Your model would be like this:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductAccessoryLink> ProductAccessoryLinks { get; set; }
}
public class Accessory
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductAccessoryLink> ProductAccessoryLinks { get; set; }
}
public class Type
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ProductAccessoryLink
{
public int ProductId { get; set; }
public int AccessoryId { get; set; }
public int TypeId { get; set; }
public int sort { get; set; }
public string notes { get; set; }
public virtual Type Type { get; set; }
public virtual Product Product { get; set; }
public virtual Accessory Accessory { get; set; }
}
And you could configure the relationships overriding the OnModelCreating method on your context this way:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ProductAccessoryLink>().HasKey(i => new { i.ProductId, i.AccesoryId, i.TypeId});
modelBuilder.Entity<ProductAccessoryLink>()
.HasRequired(i => i.Product)
.WithMany(k => k.ProductAccessoryLinks)
.HasForeignKey(i=>i.ProductId);
modelBuilder.Entity<ProductAccessoryLink>()
.HasRequired(i => i.Accesory)
.WithMany(k => k.ProductAccessoryLinks)
.HasForeignKey(i=>i.AccesoryId);
modelBuilder.Entity<ProductAccessoryLink>()
.HasRequired(i => i.Type)
.WithMany()
.HasForeignKey(i=>i.TypeId);
}
EF lets you configure directly many-to-many relationships of the way you were attempting to. Thereby EF is responsible for build a join table in the database with the appropriate keys of the tables it’s joining. (The keys are both primary keys of the join table and foreign keys pointing to the joined tables). That lets you to get your data across the join table without you having to be aware of its presence. But when you want to personalize that table (adding, for example, some additional properties), you need to map it explicitly as I show above.
Update
That exception is caused when you have multiple paths of cascade deletes that could end trying to delete the same row in the Types table. To resolve that problem I recommend you check my answer in this post
So i am trying to create a table with a foreign key, but it always says that it cannot find the foreign key. heres the code:
public class Tecnologies
{
[Key]
public int TecId { get; set; }
[Required]
public String Name { get; set; }
}
this one works, then i try to create this one:
public class UserTecnologies
{
[Key]
public int UserTecId { get; set; }
[ForeignKey("Id")]
public UserProfile User { get; set; }
[ForeignKey("TecId")]
public virtual Tecnologies Tecnology { get; set; }
[Required]
public int Rating { get; set; }
}
and it gives me the error :
The ForeignKeyAttribute on property 'Tecnology' on type 'ESW_CloddOffice.Models.UserTecnologies' is not valid. The foreign key name 'TecId' was not found on the dependent type 'ESW_CloddOffice.Models.UserTecnologies'. The Name value should be a comma separated list of foreign key property names.
The names are correct, what am i missing ?
Okay, i found what i was doing wrong. Heres the correct code:
public class UserTecnologies
{
[Key]
public int UserTecId { get; set; }
[ForeignKey("UserProfile")]
public virtual int UserProfileId { get; set; }
[ForeignKey("Tecnology")]
public virtual int TecnologyId { get; set; }
public virtual Tecnologies Tecnology { get; set; }
public virtual UserProfile UserProfile { get; set; }
[Required]
public int Rating { get; set; }
}
Was creating the foreign key the wrong way .
The ForeignKey attribute requires that an actual property on the entity match the name you pass in. It doesn't just tell EF what to call the key at the database level.
You either need to actually add a TecId property:
public int TecId { get; set; }
Or use fluent configuration, instead:
modelBuilder.Entity<UserTechnologies>()
.HasRequired(c => c.Technology)
.WithMany()
.Map(m => m.MapKey("TecId"));
I'm using ASP.NET MVC 4 Entity Framework 5 to generate a code first migration for a database structure of the below classes that have relationships between them. However I've encountered a problem where it's causing this error whenever I try to update my database from the migration:
Paste Bin of the migration file can be found here: http://pastebin.com/ngXacrKV
Error returned:
Introducing FOREIGN KEY constraint 'FK_dbo.Bookings_dbo.Rooms_RoomId' on table 'Bookings' 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.
Bunk.cs
public class Bunk
{
[Key]
public int BunkId { get; set; }
public BunkStatus BunkStatus { get; set; }
[ForeignKey("Room")]
public int RoomId { get; set; }
public virtual Room Room { get; set; }
// Added for convenience
public virtual ICollection<Booking> Bookings { get; set; }
}
Room.cs
public class Room
{
[Key]
public int RoomId { get; set; }
public string RoomName { get; set; }
public Gender RoomGender { get; set; }
public RoomStatus RoomStatus { get; set; }
public virtual ICollection<Bunk> Bunks { get; set; }
// Added for convenience
public virtual ICollection<Booking> Bookings { get; set; }
}
Bookings.cs
public class Booking
{
[Key]
public int BookingId { get; set; }
//[ForeignKey("UserProfile")]
//public int UserProfileId { get; set; }
//public UserProfile UserProfile { get; set; }
[ForeignKey("Bunk")]
public int BunkId { get; set; }
public Bunk Bunk { get; set; }
public int Duration { get; set; }
[ForeignKey("Preferred_Room")]
public int RoomId { get; set; }
public Room Preferred_Room { get; set; }
public Decimal Price { get; set; }
public BookingStatus BookingStatus { get; set; }
}
What would the best work around be to remove this issue without causing too much disturbance to the original class structure. I'm not too worried about adding new joining tables as long as I can still access the code in a Lazy loading way in my controllers/view models.
you can try to specify Fluent API to set no cascade delete
public class YOURContext: DbContext{
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
// here is where fluent API goes.
// I suspected the error is EF wanting a NO cascade delete. Hence the suggestion to try
entity<Booking>.HasOptional(t => t.Bunk)
.WithOptionalPrincipal()
.WillCascadeOnDelete(false); // <<<<<< this is the option to try.
// you may also need to try the same with Preferred_Room as well.
If no cascade delete is required then in Database context class we can set cascade delete to false for all the relationships by overriding OnModelCreating method as given below. Default EF makes it cascade delete that is why you are getting exception of cycles or multiple delete path.
public class MyContext: DbContext
{
//db sets defined
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PrimaryKeyTable>().HasMany(x => x.ChildTableCollection).WithRequired(x =>
Key).WillCascadeOnDelete(false);
//In your case
modelBuilder.Entity<Bunk>().HasMany(x => x.Bookings).WithRequired(x =>
x.BunkId).WillCascadeOnDelete(false);
// same for room if required.
}
}
How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs?
At present, I have:
public class Status
{
[Required]
public int StatusId { get; set; }
[Required]
[DisplayName("Status")]
public string Name { get; set; }
}
public class Restuarant
{
public int RestaurantId { get; set; }
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Telephone { get; set; }
[Required]
public int StatusId { get; set; }
public List<Menu> Menus { get; set; }
// NAVIGATION PROPERTIES
public virtual Status Status { get; set; }
}
public class Menu
{
public int MenuId { get; set; }
[Required]
public int RestaurantId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int StatusId { get; set; }
// NAVIGATION PROPERTIES
public virtual Status Status { get; set; }
public virtual Restaurant Restaurant { get; set; }
}
And my DbContext:
public class MenuEntities : DbContext
{
public DbSet<Status> Statuses { get; set; }
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<Menu> Menus { get; set; }
}
As you can see:
a Restaurant has many menus
a Restaurant has one status
a Menu belongs to 1 restaurant
Both Restaurants and Menus have 1 status. (Live, Invisible, Draft)
Naturally, if a status is deleted, I certainly don't want to cascade as this will muck everything up.
UPDATE:
Mark Oreta mentions using the following in his example below:
modelBuilder.Entity<FirstEntity>()
.HasMany(f => f.SecondEntities)
.WithOptional()
.WillCascadeOnDelete(false);
Where do I put this code? Within my MenuEntities / DbContext class?
Can anybody provide an example of this being used?
UPDATE:
Got this bit working now, however this has created a multiplicity constraint error when trying to seed the DB...
Multiplicity constraint violated. The role 'Menu_Status_Source' of the relationship 'LaCascadaWebApi.Models.Menu_Status' has multiplicity 1 or 0..1.
My Database Initialiser:
http://pastebin.com/T2XWsAqk
You can either disable it for your entire context by removing the cascade delete convention in the OnModelCreating method:
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
or, you can do it per relationship using a fluent mapping (also in the OnModelCreating):
EDIT: you would put it in your menu entities
public class MenuEntities : DbContext
{
public DbSet<Status> Statuses { get; set; }
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<Menu> Menus { get; set; }
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<Menu>()
.HasRequired( f => f.Status )
.WithRequiredDependent()
.WillCascadeOnDelete( false );
modelBuilder.Entity<Restaurant>()
.HasRequired( f => f.Status )
.WithRequiredDependent()
.WillCascadeOnDelete( false );
}
}
Just make the FK property nullable, then the cascade delete will be gone.
public int? StatusId { get; set; }
After making the changes to the model, make sure you regenerate the migration file by adding the -Force parameter.
Add-Migration MigrationName -Force
add this line to end of the field in the context;
.OnDelete(DeleteBehavior.Restrict);
Put this into your MenuEntities class (class that descend from DbContext):
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}