我正在尝试使用 Code First 设置 TPC 继承。我有三级继承权。
抽象类A,具体类B继承自A,类C继承自B。
A 类属性:ID、CreatedBy 和 CreatedOn。
B 类属性:FirstName、LastName、BirthDate
C 类属性:约会日期、状态
我希望在数据库中创建 B 类和 C 类的表,并使用数据库生成的标识。
我的上下文类中有以下代码:
public DbSet<B> BList { get; set; }
public DbSet<C> CList { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<B>().ToTable("BClass");
modelBuilder.Entity<C>().ToTable("CClass");
}
我有一个 contextinitializer 类,它重写 Seed 方法以填充数据库中的一些测试数据。
protected override void Seed(TestContext context)
{
TestDataClass testData = new TestDataClass();
List<B> bList= testData.GetBList();
foreach (B objB in bList)
{
context.BList.Add(objB);
}
List<C> cList = testData.GetCList();
foreach (C objC in cList)
{
context.CList.Add(objC);
}
context.SaveChanges();
}
这会在数据库中创建表 BClass 和 CClass,但问题是,如果我在 BClass 中插入 10 行并在 CClass 中插入相应的 10 行,则实体框架会在 BClass 中插入 20 行,其中 10 行包含实际值,10 行包含 Null。 CClass 给出了 10 行的预期结果。
第二个问题是当我从CClass查询数据时没有得到结果。我的错误消息说:
EntitySet“CClass”未在 EntityContainer 中定义
'测试上下文'。靠近简单标识符,第 1 行,第 12 列。
我正在使用 Huy Nguyen 的 Entity Framework 4 POCO、存储库和规范模式 [升级到 EF 4.1] 帖子中的代码http://huyrua.wordpress.com/2011/04/13/entity-framework-4-poco-repository-and-specification-pattern-upgraded-to-ef-4-1/我在通用存储库中的代码看起来像
public IList<TEntity> FindAll<TEntity>() where TEntity : class
{
DbContext.Set<TEntity>();
var entityName = GetEntityName<TEntity>();
return ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName).ToList<TEntity>();
}
private string GetEntityName<TEntity>() where TEntity : class
{
DbContext.Set<TEntity>();
return string.Format("{0}.{1}", ((IObjectContextAdapter)DbContext).ObjectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
}
我尝试更改表映射,但没有帮助。我不知道我在这里错过了什么,也不知道该怎么做。我急需帮助。
提前致谢。
这就是我的实际课程的样子:
public abstract class EntityBase
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public virtual long Id { get; set; }
public virtual string CreatedBy { get; set; }
public virtual DateTime? CreatedOn { get; set; }
public virtual string LastModifiedBy { get; set; }
public virtual DateTime? LastModifiedOn { get; set; }
}
public class Person: EntityBase
{
public virtual string FirstName { get; set; }
public virtual string MiddleName { get; set; }
public virtual string LastName { get; set; }
public virtual DateTime? BirthDate { get; set; }
[NotMapped]
public virtual string FullName
{
get
{
return String.Format("{0}, {1} {2}", LastName, FirstName, MiddleName);
}
}
}
public partial class Employee : Person
{
[Required]
public string EmployeeNumber { get; set; }
[Required]
public string department { get; set; }
public DateTime? JoiningDate { get; set; }
public DateTime? PromotionDate { get; set; }
}
我的上下文类现在有:
public DbSet<EntityBase> People { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Person>().ToTable("Person");
modelBuilder.Entity<Employee>().Map(m => m.MapInheritedProperties()).ToTable("Employee");
}