代码优先INSERT语句与FOREIGN KEY约束冲突 [英] Code first The INSERT statement conflicted with the FOREIGN KEY constraint
问题描述
我刚开始使用Code first方法来创建数据库。我有以下3个表:
公共类TagDatabase
{
[Key]
[DatabaseGenerated( DatabaseGeneratedOption.Identity)]
public int TagID {get;组; }
公共字符串TagName {get;组; }
public string说明{get;组; }
public int Count {get;组; }
[ForeignKey(TagTypes)]
public virtual int TagTypeID {get;组; }
public virtual ICollection< tagtypesdb> TagTypes {get;组; }
[ForeignKey(Users)]
public virtual int CreatedBy {get;组; }
public virtual UsersDb Users {get;组; }
}
公共类TagTypesDb
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TagTypeID {get ;组; }
公共字符串TagTypeName {get;组; }
}
公共类UsersDb
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserID {get ;组; }
公共字符串UserName {get;组; }
}
这里AccountHolder和Nominee都有1到*的地址替换。我用于此的流畅API代码是:
protected 覆盖 void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< tagdatabase>()
.HasOptional (a = > a.TagTypes)
.WithMany()
.HasForeignKey(u = > u.TagTypeID);
modelBuilder.Entity< tagdatabase>()
.HasRequired(a = > a.Users)
。 WithMany()
.HasForeignKey(u = > u.CreatedBy);
}
现在我的问题是每当我试图在AccountHolder或Nominee中插入数据时我都有这个例外:
TagDatabase_TagTypes ::多重性与关系TagDatabase_TagTypes中Role'TagDatabase_TagTypes_Target'中的引用约束冲突。因为从属角色中的所有属性都是不可为空的,所以主要角色的多样性必须为1。
有人可以告诉我如何解决这个问题我在这里缺少什么?
你的流利API有TagTypes选项
引用:HasOptional(a => a.TagTypes)
但是在模型外键关系似乎是强制性的。要么将Hasoptional更改为强制,要么在模型中使其可以为空。
public int? TagTypeID {get;设置;}
和/或
public int? CreatedBy {get;设置;}
I just started using Code first approach for creating databases. I have following 3 tables :
public class TagDatabase { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int TagID { get; set; } public string TagName { get; set; } public string Description { get; set; } public int Count { get; set; } [ForeignKey("TagTypes")] public virtual int TagTypeID { get; set; } public virtual ICollection<tagtypesdb> TagTypes { get; set; } [ForeignKey("Users")] public virtual int CreatedBy { get; set; } public virtual UsersDb Users { get; set; } } public class TagTypesDb { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int TagTypeID { get; set; } public string TagTypeName { get; set; } } public class UsersDb { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int UserID { get; set; } public string UserName { get; set; } }
Here AccountHolder and Nominee both have 1 to * replationship with the address. The fluent API code which i used for this is :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<tagdatabase>()
.HasOptional(a => a.TagTypes)
.WithMany()
.HasForeignKey(u => u.TagTypeID);
modelBuilder.Entity<tagdatabase>()
.HasRequired(a => a.Users)
.WithMany()
.HasForeignKey(u => u.CreatedBy);
}
Now my issue is whenever i am trying to insert data in AccountHolder or Nominee i got this exception :
TagDatabase_TagTypes: : Multiplicity conflicts with the referential constraint in Role 'TagDatabase_TagTypes_Target' in relationship 'TagDatabase_TagTypes'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
Can anybody please tell me how to solve this issue and what i am missing here ?
Your fluent API has TagTypes option
Quote:HasOptional(a => a.TagTypes)
But in the model foreign key relationship seems to be mandatory .Either change the Hasoptional to mandatory or make it nullable in the model.
public int? TagTypeID { get; set;}
and/or
public int? CreatedBy { get; set;}
这篇关于代码优先INSERT语句与FOREIGN KEY约束冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!