我的Domain-project中有这个类:
public class Person: EntityObject
{
public int ID { get; set; }
public string Name { get; set; }
}
我的DataAccess项目中有一个存储库:
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();
}
}
我的dbcontext也在DataAccess项目中,如下所示:
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; }
}
我添加了一个扩展方法来提供审计跟踪。
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)
{
// get a list of changes to audit
IEnumerable<ObjectStateEntry> entities =
from e in context.ObjectStateManager.GetObjectStateEntries(
EntityState.Modified | EntityState.Deleted)
where
e.IsRelationship == false &&
typeof(TFromType).IsAssignableFrom(e.Entity.GetType())
select e;
foreach (ObjectStateEntry item in entities)
{
// map the changed item to an audit record entry
TToType auditRecord = mapping(item.OriginalValues, item.State);
// and add it to the object context so that it is persisted for us
addToContext(auditRecord);
}
}
public static T Field<E, T>(this IDataRecord record, Expression<Func<E, T>> propertySelector)
where E : EntityObject
{
MemberExpression memberExpression = propertySelector.Body as 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;
}
}
现在,当我运行我的应用程序时,我收到以下错误:
类型&#39; System.InvalidOperationException&#39;的例外情况发生在 EntityFramework.dll但未在用户代码中处理
其他信息:类型&#39; CoBen.Dossier.Domain.Person&#39;不是 映射。检查是否未使用明确排除类型 Ignore方法或NotMappedAttribute数据注释。验证 类型被定义为一个类,不是原始的或通用的,并且确实如此 不是从EntityObject继承。
我做错了什么?