对不起,但我对表格拆分感到困惑!
我有 Product 和 ProductDetail 实体,映射到表 Product。
当我加载 Products 集合时,也会加载 ProductDetails。
有人可以解释我如何防止 ProductDetails 被加载,直到我真的想要它们?谢谢!
using (var ctx = new Context(cs))
{
var pc = ctx.Products.Local.Count();
var pdc = ctx.ProductDetails.Local.Count();
Assert.IsTrue(pc == 0);
Assert.IsTrue(pdc == 0);
ctx.Products.Load();
pc = ctx.Products.Local.Count();
pdc = ctx.ProductDetails.Local.Count();
Assert.IsTrue(pc >= 10); //OK so far
Assert.IsTrue(pdc == 0); //no, they are all there
}
我的上下文类和实体:
public class Context:DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public Context():base()
{
}
public Context(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
public DbSet<Product> Products { get; set; }
public DbSet<ProductDetail> ProductDetails { get; set; }
}
[Table("Product")]
public class Product
{
[Key]
public virtual int ProductId { get; set; }
public virtual string Name { get; set; }
}
[Table("Product")]
public class ProductDetail:Product
{
//[Key]
//public virtual int ProductId { get; set; }
//public virtual string Name { get; set; }
public virtual string Description { get; set; }
}