0

.NETコアをEFで使用していて、データベースに追加しようとしたときに問題が発生しました。他のエンティティタイプを含むMapという複雑なJSONオブジェクトがあります。このエラーはSourceFieldで発生します.SourceFieldはRuleSourceFieldsおよびConditionsにあり、マップ内にあります。私は外国とプライマリのキーが正しいことを確認するためにチェックしましたが、それはそれを修正しませんでした。私が間違っていない限り、仮想メンバーを持つエンティティーを追加しても、EFはそのサブタイプを追加しようとしないはずです。このエラーは、SaveChanges()でのみ発生します。Entity Framework「ID列に明示的な値を挿入できません」

エラー:私は送るよJSONの

{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} 

例:

{ 
    "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" 
       } 
      } 
     ] 
     } 
    ] 
} 

Modelクラス例:

SourceField.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; } 

答えて

0

私は右、あなたのJSONに表示されますsourceFieldId=28sourceFieldがすでに存在し、あなたがそれを挿入したくないと仮定しますか?また、親エンティティはJSON(データベースコンテキストからフェッチしていない)から入力したため、コンテキストに添付されません。

あなたの子供SourceFieldエンティティのEFのトラッキング状態が正しくUnchangedModifiedが、Addedように設定されていないようです。これは、接続されていないシナリオでエンティティグラフを処理するときに発生する問題の1つです。 GraphDiffを使用してこれを修正することができます。GraphDiffは、子ツリーを通過し、各子の状態が正しく設定されていることを確認します。または、これを自分で実装して、子供の追跡状態を手動で更新することもできます。

これがなぜ起こり、どのように修正するかについての詳細はthis answerです。また

、あなたはJSONではなく、全体のナビゲーションエンティティsourceFieldの財産sourceFieldIdの値だけが含まれているかどうおそらくあなたは、この問題を回避する、sourceField子の内容を更新するように意図されていない場合。

仮想ナビゲーションプロパティを使用する目的は、レイジーロードを実行するときにEFがプロキシエンティティを使用できるようにすることです。私が知る限り、子エンティティの追加または変更を許可することには何の影響もありません。

+0

ええ、それは私がそれを挿入したくないという最初の点です。 IDを渡すだけで述べた解決策は、私のプロジェクトの他のセクションでやったことです。私にとってうまくいっているようだ。ありがとう! – Deej

+0

私はうれしいです!どういたしまして – Diana

関連する問題