簡體   English   中英

使用Entity Framework和動態Json將范圍標識插入第二個表

[英]Inserting Scope identity into second table using Entity Framework and dynamic Json

我有兩個表,如下所示1)客戶

customerId  Int (primary key)
customerName    Varchar(50)
Age Int

2)客戶貸款

Id  Int 
customerId  Int (Foreign key)
customerName    Varchar(50)
Age Int
Loan    float

從我的jquery中,我在webservice webmethod中以動態json對象的形式獲取多個記錄,如下所示(InsertData)。 通過使用IList和Entity框架,我可以插入多個記錄。

我的要求是在插入客戶記錄時,我要從客戶表中插入一些字段,並在客戶貸款表中插入額外的字段。

最后,我要插入從客戶表生成的cutomerId以及CustomerLoan表中的其他幾個字段。

例如:客戶

customerId  customerName    Age
100 John    32
101 Jacob   35

例如:客戶貸款

Id  customerId  customerName    Age Loan
1   100 John    32  1500
2   101 Jacob   35  2000



[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public dynamic InsertData(int revision, int appID, dynamic jsonMaster)
{
    dynamic json = jsonMaster;

    IList<Customer> customers = ((object[])json).Select(t => new Customer
    {
        customerId = Convert.ToInt32((((IDictionary)t)["customerId"]) ?? -1),            
        customerName = ((((IDictionary)t)["customerName"]) ?? "").ToString(),
        Age = Convert.ToInt32(((IDictionary)t)["Age"]), 
        Revision = Convert.ToInt32((((IDictionary)t)["Revision"])),            
    }).ToList(); ;


    lock (_updatePointsLock)
    {
        using (CustomerEntities context = new CustomerEntities())
        {
            int currentRevision = context.Customer.Max(x => x.Revision) ?? 0;
            if (currentRevision >= revision)
            {
                foreach (Customer cobj in customers)
                {
                    Customer obj = context.Customer.Where(x => x.customerId == cobj.salesMasterId).FirstOrDefault();
                    if (obj == null)
                    {
                        cobj.Revision = currentRevision + 1;                            
                        context.Customer.Add(cobj); 

            **CustomerLoan objLoan = new CustomerLoan();
            objLoan.customerId = cobj.customerId;  
            objLoan.customerName = cobj.customerName;
            objLoan.Age = cobj.Age;
            objLoan.customerLoan = 1500;
            context.CustomerLoan.Add(objLoan);**




                    }
                    else
                    {
                        obj.customerName = cobj.customerName;
                        obj.Age = cobj.Age;                            
                        obj.Revision = currentRevision + 1;                          

                    }
                }
                context.SaveChanges();

                return new
                {
                    Revision = currentRevision + 1,
                    Customer = context.Customer.Where(x => x.Revision > revision).Select(x => new
                    {
                        x.customerId,
                        x.customerName,
                        x.Age,                            
                        Revision = x.Revision,                            
                    }).ToList()
                };
            }
            else
            {
                return new { Revision = revision };
            }
        }
    }

}

使用上述代碼(-1),將值插入到customerLoan表的customerId字段中。 如果創建要插入的對象,則無法獲取客戶的foreach值之外的對象。 如果有人可以幫助在客戶貸款中插入身份值客戶表,我們將不勝感激。

首先使用context.SaveChanges()將客戶對象保存到數據庫中; 然后嘗試添加客戶貸款(現在您應該可以找到客戶ID)並使用context.SaveChanges();再次保存到數據庫中。

它可能有其他方法可以做到,但這就是我所知道的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM