与 FOREIGN KEY 冲突
Posted
技术标签:
【中文标题】与 FOREIGN KEY 冲突【英文标题】:Conflicted with the FOREIGN KEY 【发布时间】:2011-10-17 15:04:28 【问题描述】:保存对象对时出错。
代码
数据类
public class User
public Guid Id get; set;
public string FirstName get; set;
public string LastName get; set;
public string Email get; set;
public string Password get; set;
public bool IsApproved get; set;
public bool IsBlock get; set;
public bool IsGuest get; set;
public string CodeGuest get; set;
public Gender Gender get; set;
public DateTime? Birth get; set;
public virtual ICollection<Role> Roles get; set;
public virtual Couple Couple get; set;
public User()
Id = Guid.NewGuid();
public class Couple
public Guid Id get; private set;
public string UrlKeyword get; set;
public virtual User Groom get; set;
public virtual User Bride get; set;
public DateTime? Marriage get; set;
public DateTime? Dating get; set;
public DateTime? Engagement get; set;
public virtual ICollection<User> Users get; set;
public Couple()
Id = Guid.NewGuid();
上下文和配置
public class DataContext : DbContext
#region Collections
public DbSet<Role> Roles get; set;
public DbSet<User> Users get; set;
public DbSet<Couple> Couples get; set;
#endregion
public DataContext()
Database.SetInitializer(new AndMarriedInitializer());
if (!Database.CreateIfNotExists())
Database.CreateIfNotExists();
protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Configurations.Add(new UserConfiguration());
base.OnModelCreating(modelBuilder);
public class UserConfiguration : EntityTypeConfiguration<User>
public UserConfiguration()
HasKey(p => p.Id).
Property(p => p.Id)
.IsRequired();
Property(p => p.FirstName)
.HasMaxLength(60)
.IsRequired();
Property(p => p.LastName)
.HasMaxLength(120)
.IsRequired();
Property(p => p.Email)
.HasMaxLength(120)
.IsRequired();
Property(p => p.Password)
.HasMaxLength(60);
Property(p => p.CodeGuest)
.HasMaxLength(60);
HasRequired(u => u.Couple).WithRequiredPrincipal();
public class CoupleConfiguration : EntityTypeConfiguration<Couple>
public CoupleConfiguration()
HasKey(p => p.Id)
.Property(p => p.Id)
.IsRequired();
Property(p => p.UrlKeyword)
.IsRequired()
.HasMaxLength(25);
HasRequired(p => p.Groom).WithRequiredPrincipal().WillCascadeOnDelete();
HasRequired(p => p.Bride).WithRequiredPrincipal().WillCascadeOnDelete();
public class AndMarriedInitializer : DropCreateDatabaseIfModelChanges<DataContext>
protected override void Seed(DataContext context)
context.Roles.Add(new Role
Name = Constants.RoleAdmin
);
context.Roles.Add(new Role
Name = Constants.RoleCouple
);
context.Roles.Add(new Role
Name = Constants.RoleGuest
);
context.SaveChanges();
base.Seed(context);
问题
不知道配置是否正确,但是我们与用户的情侣关系是:1对1。
不太明白WithRequiredPrincipal
和WithRequiredDependent
是怎么回事
错误
关于 SaveChanges() =>
The INSERT statement conflicted with the FOREIGN KEY constraint "User_Couple". The conflict occurred in database "andmarried", table "dbo.Users", column 'Id'.
The statement has been terminated.
【问题讨论】:
【参考方案1】:有了这行代码:
public UserConfiguration()
//...
HasRequired(u => u.Couple).WithRequiredPrincipal();
//...
您要求所有用户始终“耦合”。
那么,当您提交更改时,您创建的所有用户都结婚了吗?如果不是,请删除该外键约束。 (把这对夫妇留在里面,你应该没事)
【讨论】:
是的,但我想要的是每个用户都与一对相关联。定义是情侣还是客人的是角色; 在任何情况下,听起来您都在提交一个数据库状态,其中用户存在而没有夫妻。也许自动提交是一个问题?你能展示实际调用这些对象的代码吗?即:正在创建和保存什么,然后实际行因异常而失败?注意:根据您的上一条评论,用户 Couple 听起来像是 2 1 或许多 1。public Couple Add(Couple entity) // Add Roles var role = (from r in RoleRepository.GetAll() where r.Name == Constants.RoleCouple select r).SingleOrDefault(); entity.Bride.Roles.Add(role); entity.Groom.Roles.Add(role); //entity.Users.Add(entity.Bride); //Problem here //entity.Users.Add(entity.Groom); //Problem here return Repository.Add(entity);
查看//Problem here
行 不知道我是否将此行注释掉,或者删除注释。关系是 1 个用户的 2 对情侣
如果我删除评论会出现以下错误:Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
你有没有为用户情侣关系尝试过WithMany()?以上是关于与 FOREIGN KEY 冲突的主要内容,如果未能解决你的问题,请参考以下文章
消息 547,INSERT 语句与 FOREIGN KEY 约束冲突
SqlException:INSERT 语句与使用 EF 代码优先 C# 的 FOREIGN KEY 约束冲突
当插入为 NULL 时,“INSERT 语句与 FOREIGN KEY 约束冲突”
如何编写检查以避免消息“INSERT语句与FOREIGN KEY约束冲突”?