我将 .NET Core 与 EF 结合使用,但在尝试添加到数据库时遇到了问题。我有一个名为 Map 的复杂 JSON 对象,其中包含其他实体类型。错误出现在 SourceField 中,它可以在 RuleSourceFields 和 Conditions 中,它们本身在 Map 中。我已经检查以确保我的外键和主键正确无误,但这并没有解决问题。除非我弄错了,否则我认为添加具有虚拟成员的实体不应导致 EF 尝试添加该子实体?请注意,此错误仅发生在 SaveChanges() 上。
错误:
{System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'SourceFields' when IDENTITY_INSERT is set to OFF.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
at System.Data.SqlClient.SqlDataReader.TryHasMoreResults(Boolean& moreResults)
at System.Data.SqlClient.SqlDataReader.TryNextResult(Boolean& more)
at System.Data.SqlClient.SqlDataReader.NextResult()
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(DbDataReader reader)
ClientConnectionId:3bc06159-276e-4a39-bbb9-7c66999e255f
Error Number:544,State:1,Class:16}
我发送的 JSON 示例:
{
"description":"test",
"effective_Date":"2016-10-23T18:59:49.3855195",
"active":true,
"transformations":[
{
"description":"sdfsdf",
"rule":{
"rule_Value":"",
"alt_Value":"",
"rule_Operation":"sfield",
"targetField":{
"targetFieldId":1,
"name":"TEST",
"datatype":"text",
"active":true,
"seqNum":1,
"rules":[
],
"targetId":1,
"target":null,
"created_By":"Anonymous",
"creation_Date":"2016-10-23T18:59:49.3855195",
"date_Modified":"2016-10-23T18:59:49.3855195",
"modified_By":"Anonymous"
},
"ruleSourceFields":[
]
},
"conditions":[
{
"seqNum":1,
"chain_Operation":"or",
"left_Paren":"(",
"operation":"!=",
"cond_Value":"sdf",
"right_Paren":")",
"sourceField":{
"sourceFieldId":28,
"name":"N/A",
"datatype":"text",
"active":true,
"seqNum":2,
"conditions":[
],
"ruleSourceFields":[
],
"sourceId":19,
"source":null,
"created_By":"Anonymous",
"creation_Date":"2016-10-12T04:51:03.3311291",
"date_Modified":"2016-10-12T04:51:03.3311291",
"modified_By":"Anonymous"
}
}
]
}
]
}
模型类示例:
源字段.cs
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SourceFieldId { get; set; }
public string Name { get; set; }
public string Datatype { get; set; }
public bool Active { get; set; }
public int SeqNum { get; set; }
[ForeignKey("SourceFieldId")]
public virtual ICollection<Condition> Conditions { get; set; }
[ForeignKey("SourceFieldId")]
public virtual ICollection<RuleSourceField> RuleSourceFields { get; set; }
public int SourceId { get; set; }
public virtual Source Source { get; set; }
条件.cs
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ConditionId { get; set; }
public int SeqNum { get; set; }
public string Chain_Operation { get; set; }
public string Left_Paren { get; set; }
public string Operation { get; set; }
public string Cond_Value { get; set; }
public string Right_Paren { get; set; }
public int SourceFieldId { get; set; }
public virtual SourceField SourceField { get; set; }
public int TransformationId { get; set; }
public virtual Transformation Transformation { get; set; }
最佳答案
我假设您的 JSON 中出现的带有 sourceFieldId=28
的 sourceField
已经存在,您不想插入它,对吗?并且您的父实体没有附加到上下文,因为您只是从 JSON 中填充它(您没有从数据库上下文中获取它)。
您的子 SourceField
实体的 EF 跟踪状态似乎未正确设置为 Unchanged
或 Modified
,而是设置为 已添加
。这是在断开连接的场景中处理实体图时遇到的问题。您可以使用 GraphDiff 解决此问题,它遍历您的子树并确保正确设置每个 child 的状态。或者您可以自己实现并手动更新您 child 的跟踪状态。
关于为什么会发生这种情况以及如何解决的更多信息,请参阅 this answer .
此外,如果您不打算更新 sourceField
子项的内容,如果您的 JSON 仅包含属性 sourceFieldId
而不是整个导航实体 sourceField
。
使用虚拟导航属性的目的是让EF在做Lazy Loading时可以使用代理实体。据我所知,它们对允许添加或修改子实体没有任何影响。
关于.net - Entity Framework "Cannot insert explicit value for for identity column",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208578/