c# - The type not mapped issue -
i have class in domain-project :
public class person: entityobject { public int id { get; set; } public string name { get; set; } }
i have repository in dataaccess-project:
public class personrepository { databasecontext dbcontext { get; } public personrepository(databasecontext dbcontext) { this.dbcontext = dbcontext; } public virtual ienumerable<person> getall() { return dbcontext.persons.tolist(); } public virtual void savechanges(person persoon) { // configure auditing dbcontext.audit<person, personhistory>( (record, action) => new personhistory() { id = record.field<person, int>(f => f.id), name = record.field<person, string>(f => f.name), createddate = datetime.now, createdby = "simon", changedby = "simon", changeddate = datetime.now, changetype = action.tostring() }, (ph) => dbcontext.personhistory.add(ph)); dbcontext.savechanges(); } }
my dbcontext in dataaccess-project , looks follows:
public class databasecontext : dbcontext { public databasecontext() { database.setinitializer<databasecontext>(null); } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<pluralizingtablenameconvention>(); } public virtual dbset<person> persons { get; set; } public virtual dbset<personhistory> personhistory { get; set; } }
i've added extension-method provide audittrailing.
public static class dbcontextextension { public static void audit<tfromtype, ttotype>(this dbcontext context,func<idatarecord, entitystate, ttotype> mapping,action<ttotype> addtocontext) { objectcontext ctx = ((iobjectcontextadapter)context).objectcontext; ctx.savingchanges += new eventhandler((o, e) => createauditrecord<tfromtype, ttotype>(ctx, mapping, addtocontext)); } private static void createauditrecord<tfromtype, ttotype>( objectcontext context, func<idatarecord, entitystate, ttotype> mapping, action<ttotype> addtocontext) { // list of changes audit ienumerable<objectstateentry> entities = e in context.objectstatemanager.getobjectstateentries( entitystate.modified | entitystate.deleted) e.isrelationship == false && typeof(tfromtype).isassignablefrom(e.entity.gettype()) select e; foreach (objectstateentry item in entities) { // map changed item audit record entry ttotype auditrecord = mapping(item.originalvalues, item.state); // , add object context persisted addtocontext(auditrecord); } } public static t field<e, t>(this idatarecord record, expression<func<e, t>> propertyselector) e : entityobject { memberexpression memberexpression = propertyselector.body memberexpression; memberinfo propertyinfo = memberexpression.member; int fieldindex = record.getordinal(propertyinfo.name); return field<t>(record, fieldindex); } public static t field<t>(this idatarecord record, int ordinal) { object value = record.isdbnull(ordinal) ? null : record.getvalue(ordinal); return (t)value; } }
now when run application following error:
an exception of type 'system.invalidoperationexception' occurred in entityframework.dll not handled in user code
additional information: type 'coben.dossier.domain.person' not mapped. check type has not been explicitly excluded using ignore method or notmappedattribute data annotation. verify type defined class, not primitive or generic, , not inherit entityobject.
what doing wrong ?
Comments
Post a Comment