Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# POCO实体框架与多态性_C#_Oop_Ef Code First_Poco - Fatal编程技术网

C# POCO实体框架与多态性

C# POCO实体框架与多态性,c#,oop,ef-code-first,poco,C#,Oop,Ef Code First,Poco,我首先使用实体框架代码,然后使用下一个POCO类: public class Product { public int ProductID { get; set; } public int Title{ get; set; } public int Price { get; set; } public int ProductCategoryID { get; set; } public v

我首先使用实体框架代码,然后使用下一个POCO类:

 public class Product 
    {       
        public int ProductID { get; set; }
        public int Title{ get; set; }
        public int Price { get; set; }

        public int ProductCategoryID { get; set; }
        public virtual ProductCategory ProductCategory { get; set; }
    }

  public class ProductCategory 
    {
        public int ProductCategoryID { get; set; }        
        public int Title{ get; set; }
        public virtual ICollection<Product> Products { get; set; }
    }
但我不喜欢这种方法,我希望使用OOP,并有如下内容:

public class Dress : Product 
{
  public override int void GiveSale()
   {
      return this.Price * 0,8;
   }
}

public class Shoes : Product 
{
  public override int void GiveSale()
   {
      return 0;
   }
}

如何使用Entities框架实现这一点,并节省实体的易用性

EF将对象映射到表中,这是一种非常明显的方法,但当涉及到多态性时,DB并不是我们最好的朋友

注意EF如何使实体部分类。这是为你扩展它们。 因此,您可以编写扩展类并将虚拟方法放在那里,然后继续实现其他派生类

假设这是生成的EF类:

public partial class Entity
{
    public int ID { get; set; }
    public string Name { get; set; }
    ...
}
然后,在另一个文件上,将扩展类写在与实体相同的命名空间下:

public partial class Entity
{
      public virtual int GiveSale()
      {
           return 0;
      }
}
继续您的层次结构的其余部分:

public class Derived : Entity
{
     public override int GiveSale()
     {
           return your calculation;
     }
}

这样,您仍然拥有entity framework类,您可以自由扩展它,甚至将其作为层次结构的基类。

您可以尝试对对象使用EF继承。它也有代码优先的版本-

根据您的需要,有3种不同的方法:

  • 每个层次的表-当所有对象都与同一个表相关时
  • 每种类型的表-当所有类在共享表中都可以有公共数据,并且所有特定字段都映射到另一个表时
  • 每个混凝土类别的表-当所有类别都有自己的表时。在这种情况下,公共数据结构将在每个表中重复
  • 您需要根据OnModelCreating方法中的一些数据映射衣服、鞋子实体。例如:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       modelBuilder.Entity<Product>()
                .Map<Dress>(m => m.Requires("ProductCategoryID").HasValue(1))
                .Map<Shoes>(m => m.Requires("ProductCategoryID").HasValue(2));
    }
    
    模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity()
    .Map(m=>m.Requires(“ProductCategoryID”).HasValue(1))
    .Map(m=>m.Requires(“ProductCategoryID”).HasValue(2));
    }
    
    在这种情况下,当您从DBContext加载它们时,它将自动创建所需的类型:

    List<Product> products = dbContext.Products.ToList();
    foreach(var product in products)
    {
        var sale = product.GiveSale();
        if (product is Dress) Console.WriteLine("It's dress!");
        if (product is Shoes) Console.WriteLine("It's a pair of shoes!");       
    }
    
    List products=dbContext.products.ToList();
    foreach(产品中的var产品)
    {
    var sale=产品。GiveSale();
    如果(产品是裙子)控制台。WriteLine(“它是裙子!”);
    如果(产品是鞋)控制台。WriteLine(“这是一双鞋!”);
    }
    
    然后,您可以首先通过调用SaveChanges更新数据,并将Dress\Shoes作为常规Poco对象保存在代码中

    List<Product> products = dbContext.Products.ToList();
    foreach(var product in products)
    {
        var sale = product.GiveSale();
        if (product is Dress) Console.WriteLine("It's dress!");
        if (product is Shoes) Console.WriteLine("It's a pair of shoes!");       
    }