بررسی خطای cycles or multiple cascade paths و یا cyclical reference در EF Code first
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۱۱/۲۸ ۱۳:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity.ModelConfiguration;
namespace EF_General.Models.Ex18
{
public class UserProfile
{
public int UserProfileId { set; get; }
public string UserName { set; get; }
[ForeignKey("CartableId")]
public virtual Cartable Cartable { set; get; } // one-to-zero-or-one
public int? CartableId { set; get; }
public virtual ICollection<Doc> Docs { set; get; } // one-to-many
}
public class Doc
{
public int DocId { set; get; }
public string Title { set; get; }
public string Body { set; get; }
[ForeignKey("UserProfileId")]
public virtual UserProfile UserProfile { set; get; }
public int UserProfileId { set; get; }
public virtual ICollection<Cartable> Cartables { set; get; } // many-to-many
}
public class Cartable
{
public int CartableId { set; get; }
[ForeignKey("UserProfileId")]
public virtual UserProfile UserProfile { set; get; }
public int UserProfileId { set; get; }
public virtual ICollection<Doc> Docs { set; get; } // many-to-many
}
public class UserProfileMap : EntityTypeConfiguration<UserProfile>
{
public UserProfileMap()
{
this.HasOptional(x => x.Cartable)
.WithRequired(x => x.UserProfile)
.WillCascadeOnDelete();
}
}
public class MyContext : DbContext
{
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Doc> Docs { get; set; }
public DbSet<Cartable> Cartables { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserProfileMap());
base.OnModelCreating(modelBuilder);
}
}
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
}
public static class Test
{
public static void RunTests()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
using (var context = new MyContext())
{
var user = context.UserProfiles.Find(1);
if (user != null)
Console.WriteLine(user.UserName);
}
}
}
}Introducing FOREIGN KEY constraint 'FK_DocCartables_Cartables_Cartable_CartableId' on table 'DocCartables' 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.
public int? UserProfileId { set; get; }The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = ]
modelBuilder.Entity<Employee>()
.HasRequired(e => e.SecondTeam)
.WithMany(t => t.SecondEmployees)
.HasForeignKey(e => e.FirstTeamId)
.WillCascadeOnDelete(false); protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts)
.OnDelete(DeleteBehavior.SetNull);
}