【问题标题】:AttachAll exception: type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitlyAttachAll 异常:无法从用法中推断出方法的类型参数。尝试明确指定类型参数
【发布时间】:2011-12-02 02:22:19
【问题描述】:

场景: 尝试在我的 LinqToSql DataContext 对象中的表上调用 .AttachAll 方法。

这是相关的简化sn-p:

  public void Update(Customer cust){
    MyDataContext db = new MyDataContext();            
    db.CustomerInvoices.AttachAll(cust.Invoices);  //exception raised here!
    db.Customer.Attach(cust);
  }

编译器引发的异常

方法的类型参数 'System.Data.Linq.Table(Invoices).AttachAll(TSubEntity)(System.Collections.Generic.IEnumerable(TSubEntity))' 不能从用法中推断出来。尝试 指定类型参数 明确的。

问题:正确投射集合的正确方法是什么?除了演员表还有其他解决方案吗?

【问题讨论】:

  • 什么是 cust.Invoices? IEnumerable?
  • 感谢 Zyph。 Invoices 是 .dbml 中定义的 Invoice 类型。

标签: linq-to-sql


【解决方案1】:

Tf cust.Invoices 已经引用了 CustomerInvoices 表的实例,只是在做 db.Customers.Attach(cust); db.Update();应该是您需要做的所有事情。

如果 CustomerInvoices 与 Customer.Invoice 的类型不同,您可能需要遍历集合,并强制转换每个集合。

【讨论】:

    【解决方案2】:
            else if (((CheckBox)item.Cells[2].FindControl("ckbSelect")).Checked == true && ((Label)item.Cells[2].FindControl("lblIsInuse")).Text == "1")
            {
                RolePage objTemp = new RolePage();
                objTemp = new Helper().GetRolePagebyID(roleId, Convert.ToInt32(item.Cells[0].Text));
                rp.RoleId = objTemp.RoleId;
                rp.PageId = objTemp.PageId;
                rp.RolePageId = objTemp.RolePageId;
                rp.CreatedOn = objTemp.CreatedOn;
                rp.Timestamp = objTemp.Timestamp; 
                //rp.RoleId = roleId;
                //rp.PageId = Convert.ToInt32(item.Cells[0].Text);
                //rp.RolePageId =Convert.ToInt32(((Label)item.Cells[2].FindControl("lblRolePageId")).Text.Trim());  
                rp.IsNew = false;
                rp.IsDeleted = false;                
                rp.UpdatedOn = DateTime.Now;  
                erp.Add(rp);               
            }
    

    【讨论】:

      【解决方案3】:
      public string Save(Role objRole)
      {
          string message = string.Empty;
          System.Data.Common.DbTransaction trans = null;    
          try
          {
              Objdb.Connection.Open();
              trans = Objdb.Connection.BeginTransaction();
              Objdb.Transaction = trans; 
              if (objRole.RoleId == 0)
              {
                  Objdb.Roles.InsertOnSubmit(objRole);
              }
              else
              {                  
                  Objdb.Roles.Attach(objRole, true);
                  Objdb.RolePages.AttachAll(objRole.RolePages.Where(a => a.IsNew == false && a.IsDeleted == false), true);
                  Objdb.RolePages.InsertAllOnSubmit(objRole.RolePages.Where(a => a.IsNew == true && a.IsDeleted == false));
                  Objdb.RolePages.DeleteAllOnSubmit(objRole.RolePages.Where(a => a.IsNew == false && a.IsDeleted == true));
              }
              Objdb.SubmitChanges();
              trans.Commit();  
              message = "Record saved successfully.";
          }
          catch (Exception ex)
          {
              trans.Rollback();
              message = "Error : " + ex.Message;
          }
          return message; 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多