i want to update a table instance by reflection
here is what i tried
var type = Assembly.GetExecutingAssembly().GetTypes()
.FirstOrDefault(t => t.Name == TableName);
object instance = Activator.CreateInstance(type);
foreach (var item in dic)
{
PropertyInfo information = type.GetProperties()
.SingleOrDefault(x => x.Name == item.Key);
information.SetValue(instance, item.Value.ToString(), null);
}
var fx = db.Set(instance.GetType());
fx.Add(instance);
result= db.SaveChanges();
its giving me following exception
InnerException = {"Violation of PRIMARY KEY constraint
'PK_primryKeyName. Cannot insert duplicate key in object 'tableName'.
The duplicate key value is (39).
\r\nThe statement has been terminated."}
its seems Entity framework considers it as an insert rather than an update
Aucun commentaire:
Enregistrer un commentaire