【问题标题】:Generic Way to Implement EF6 Code First实现 EF6 代码优先的通用方法
【发布时间】:2015-02-16 12:46:33
【问题描述】:

我使用的是 EF6 代码优先(通用存储库概念)

创建记录的示例代码:

Generic.cs:

public T Create<T>(T t) where T : class
    {
        T newEntry = this.dbContext.Set<T>().Add(t);
        this.SaveChanges();
        return newEntry;
    }

实际代码:

 var customer = new Customer{
                      Name = "Adams",
                      Amount = 1000
                    };

CustomerId是AutoIdentity列,会自动生成Id。

与产品具有一对多关系的客户表(产品表中的客户 ID FK)

现在我在产品列表中添加新的 CustomerID

var productsList = new List<Product>
{
    new Product
       {
           ProdName = "crocin", 
           Date = DateTime.Parse("2003-09-01"),
           Customer.CustomerId = newCustomerId // 
       },
    new Product
       {
           ProdName = "crocin1", 
           Date = DateTime.Parse("2003-09-01"),
           Customer.CustomerId = newCustomerId  // 
       },
    new Product
       {
           ProdName = "crocin2", 
           Date = DateTime.Parse("2003-09-01"),
           Customer.CustomerId = newCustomerId  // 
       } 
};

现在我要访问数据库两次,因为首先我得到 CustomerId,然后我将 New CustomerId 分配给所有选定的产品。

代码如下:

var newCustomer = Generic.Create<Customer>(customer);

迭代产品列表,我将 newCustomerId 分配到 Products..

foreach(var product in productList)
{
  product.CustomerId = newCustomerId;

  Generic.Create<Product>(product); 
} 

是否可以将此代码更改为更通用的方式来实现类似这样的东西..

Create(T parentTable, List<T> childTableList)
{
    // First Inserting ParentTable

     db.SaveChanges();

    //Fetching ID

    // Inserting Into ChildTables..

    db.SaveChanges();
}

请建议我扩展此代码...

【问题讨论】:

    标签: c#-4.0 generics entity-framework-6 .net-reflector


    【解决方案1】:

    问题是您缺少Customer -&gt; Products 关联。尝试在 Customer 类中添加对 Products 的引用:

    public class Customer
    {
        public Customer()
        {
            this.Products = new List<Product>();
        }
    
        // your key
        public int CustomerId { get; set; }
    
        public string Name { get; set; }
        public int Amount { get; set; }
    
        // ... rest of your data
    
        // the new association
        public virtual ICollection<Product> Products { get; set; }
    }
    

    现在您可以在一次调用中同时添加它们:

    var productsList = new List<Product>
    {
        new Product
           {
               ProdName = "crocin", 
               Date = DateTime.Parse("2003-09-01"),
               Customer.CustomerId = newCustomerId // 
           },
        new Product
           {
               ProdName = "crocin1", 
               Date = DateTime.Parse("2003-09-01"),
               Customer.CustomerId = newCustomerId  // 
           },
        new Product
           {
               ProdName = "crocin2", 
               Date = DateTime.Parse("2003-09-01"),
               Customer.CustomerId = newCustomerId  // 
           } 
    };
    
    var newCustomer = new Customer();
    
    foreach (var product in productsList)
        newCustomer.Products.Add(product);
    

    然后将它们添加到上下文中:

    var newCustomer = Generic.Create<Customer>(newCustomer);
    

    【讨论】:

    • 感谢 haim770 回复.. 我的代码工作正常.. 但我希望一切都在单一方法中.. 像这样..Create(T parentTable, List childTableList) { //首先插入 ParentTable db.SaveChanges(); //获取 ID // 插入 ChildTables.. db.SaveChanges(); } 这可能吗 ?更通用的实现方式..
    • 按照您建议的方式重构存储库会破坏存储库的非通用目的,因为它专门与Customer 需求相关联。您需要更好地理解 Object-Relational-Mapper (ORM) 的作用。它使您可以使用objects 而不是tables,并且(根据您提出的问题判断)您的Customer 绝对应该包含对相关Product 集合的引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多