我有一个基类,如下所示:
public abstract class MyBaseClass
{
public virtual DateTime UpdatedOn { get; set; }
}
然后我有一系列继承自此的实体:
public class User : MyBaseClass
{
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
}
User
的我的映射是:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserName);
Map(x => x.Password);
Map(x => x.UpdatedOn);
}
}
然后我有其他实体,以相同的方式映射 我的问题是我收到以下错误:
尝试在已添加时添加属性“UpdatedOn”。
我想这是因为我在每个实体中映射UpdatedOn
列?
我的每个表都有这个UpdatedOn列,那么我该如何映射呢?
答案 0 :(得分:2)
Use a derived class in fluent nHibernate
这个问题可以帮到你。您基本上可以使用基本流畅的nhibernate映射类来映射UpdatedOn
列,并从该基类派生所有其他映射类。
答案 1 :(得分:1)
public interface IEntity
{
DateTime CreationDate { get; set; }
DateTime UpdationDate { get; set; }
}
public interface IUser : IEntity
{
DateTime UserName { get; set; }
DateTime Password { get; set; }
}
public interface IEmployee : IEntity
{
DateTime Name { get; set; }
DateTime Key { get; set; }
}
public abstract class Entity : IEntity
{
public virtual DateTime CreationDate { get; set; }
public virtual DateTime UpdationDate { get; set; }
}
public class User : Entity, IUser
{
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
}
public class Employee : Entity, IEmployee
{
public virtual string Name { get; set; }
public virtual string Key { get; set; }
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserName);
Map(x => x.Password);
Map(x => x.CreationDate);
Map(x => x.UpdationDate);
}
}
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.Name);
Map(x => x.Key);
Map(x => x.UpdationDate);
Map(x => x.CreationDate);
}
}