在我的项目中,我有一个Base类,应该映射到数据库:
public abstract class EntityBase
{
protected EntityBase()
{
UniqueId = Guid.NewGuid();
CreationDate = DateTime.Now;
LastModified = DateTime.Now;
}
#region Properties
public Int64 Id { get; set; }
public Guid UniqueId { get; set; }
public byte[] Version { get; set; }
public DateTime CreationDate { get; set; }
public DateTime LastModified { get; set; }
#endregion
#region Navigation Properties
public virtual long CreatedBy { get; set; }
public virtual long UpdatedBy { get; set; }
#endregion
}
并且一些实体具有共享属性,因此我为那些不应该映射的实体创建了另一个基础,甚至共享的属性应该映射到它自己的实体中:
[Serializable]
public abstract class ApplicationEntityBase : EntityBase
{
#region Properties
public string Title { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Icon { get; set; }
#endregion
}
现在的问题是,当我尝试创建数据库时,Entity Framework为Entity Base创建了大约20个表!
映射是:
public class EntityMapping : EntityTypeConfiguration<EntityBase>
{
public EntityMapping()
{
ToTable("Entity");
HasKey(e => e.Id);
Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(e => e.UniqueId).IsRequired();
Property(e => e.Version).IsRequired().IsRowVersion();
Property(e => e.CreationDate).IsRequired();
Property(e => e.LastModified).IsRequired();
Property(e => e.Order);
Property(e => e.CreatedById).IsRequired();
Property(e => e.UpdatedById).IsRequired();
}
}
和Application Entity映射的示例是:
public abstract class ApplicationMappingBase<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : ApplicationEntityBase
{
protected ApplicationMappingBase()
{
//Map(m => m.ToTable(typeof(TEntity).Name));
Property(e => e.Title).IsRequired();
Property(e => e.Name).IsRequired();
Property(e => e.Description);
Property(e => e.Icon);
}
}
public class ActionMapping : ApplicationMappingBase<Entities.Core.Security.Action>
{
public ActionMapping()
: base()
{
Map(m => m.ToTable("Action"));
Property(e => e.IsGeneral).IsRequired();
Property(e => e.OnBefore).IsOptional();
Property(e => e.OnAfter).IsOptional();
Property(e => e.DoneMessage).IsOptional();
HasMany(d => d.Activities).WithRequired(t => t.Action);
}
}
你能告诉我我的代码有什么问题吗?难道我做错了什么?
我阅读了很多文章,包括http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx,但没有发现与我的情况相同