简体   繁体   English

使用Entity Framework和动态Json将范围标识插入第二个表

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

I have two tables as follows 1) Customers 我有两个表,如下所示1)客户

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

2) CustomerLoan 2)客户贷款

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

From my jquery I am getting multiple records in the form of dynamic json object in the webservice webmethod as shown below (InsertData). 从我的jquery中,我在webservice webmethod中以动态json对象的形式获取多个记录,如下所示(InsertData)。 By using IList and Entity framework I am inserting multiple records. 通过使用IList和Entity框架,我可以插入多个记录。

My requirement here is while inserting customers record, I want to inert few fields from customer table and extra fields in customerloan table. 我的要求是在插入客户记录时,我要从客户表中插入一些字段,并在客户贷款表中插入额外的字段。

Bottom line I want to insert cutomerId generated from customer table with few more fields in CustomerLoan table. 最后,我要插入从客户表生成的cutomerId以及CustomerLoan表中的其他几个字段。

Ex:Customer 例如:客户

customerId  customerName    Age
100 John    32
101 Jacob   35

Ex: CustomerLoan 例如:客户贷款

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 };
            }
        }
    }

} }

With the above code (-1) value inserting in customerId field in customerLoan table. 使用上述代码(-1),将值插入到customerLoan表的customerId字段中。 If create objects to insert outside the foreach values of Customers not getting. 如果创建要插入的对象,则无法获取客户的foreach值之外的对象。 If someone can help inserting identity value customer table in customerLoan highly appreciated. 如果有人可以帮助在客户贷款中插入身份值客户表,我们将不胜感激。

First save your customer object to database with context.SaveChanges(); 首先使用context.SaveChanges()将客户对象保存到数据库中; Then try to add Customer loan (now you should be able to find the customer Id) and save again to database with context.SaveChanges(); 然后尝试添加客户贷款(现在您应该可以找到客户ID)并使用context.SaveChanges();再次保存到数据库中。

It may have other ways to do it but this is the way I know. 它可能有其他方法可以做到,但这就是我所知道的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM