c# - The original state instance has the wrong type -
i exception: invalidoperationexception: original state instance has wrong type.
when using version of following cut down code:
table existing = context.tables.single(t => t.key == derivedfromtable.key); context.tables.attach((table)derivedfromtable, existing); //thrown here context.submitchanges();
where derivedfromtable derivedfromtable
, class derivedfromtable : table
.
what exception mean (as ((table)derivedfromtable) table
, existing table
) , how can resolve it?
the (table)derivedfromtable
cast meaningless, because attach()
method accepts argument of type table
widening cast implicit.
that doesn't matter, however, because linq sql checks type of passed in object dynamically, , doesn't support treating derived types if base entity (also because casting not change actual type of instance, changes static interface). if want this, you'll need first copy properties of derived instance instance of base type using automapper. example:
table existing = context.tables.single(t => t.key == derivedfromtable.key); table table = mapper.map<derivedfromtable, table>(derivedfromtable); context.tables.attach(table , existing); context.submitchanges();
Comments
Post a Comment