c# - DbSet.Add to ignore properties that is not set -
using efcore, model has table foreign key constraints, refers table, column nullable. models this:
public class order { [key] public int id { get; set; } public int code { get; set; } public int amount { get; set; } ... // other fields } public class ordercode { [key] public int id { get; set; } public int code { get; set; } public string description { get; set; } } order o = new order { amount = 1 }; // code not set , default null in db orders.add(o); db.savechanges(); // failure
so case code
empty , default null
in database, when add objects via dbset.add
, threw exception:
microsoft.entityframeworkcore.dbupdateexception: error occurred while updating entries. see inner exception details. ---> system.data.sqlclient.sqlexception: insert statement conflicted foreign key constraint "fk_orders_code". conflict occurred in database "store", table "dbo.ordercode", column 'id'.
it looks efcore try add field in insert statement not set, there way avoid that?
i think should use int? code in model. default value int 0 entity framework sets code field.
public class order { [key] public int id { get; set; } public int? code { get; set; } public int amount { get; set; } ... // other fields }
Comments
Post a Comment