C# 使用实体框架和动态Json将作用域标识插入第二个表

C# 使用实体框架和动态Json将作用域标识插入第二个表,c#,.net,json,entity-framework-4,webmethod,C#,.net,Json,Entity Framework 4,Webmethod,我有两张桌子如下 1) 顾客 customerId Int (primary key) customerName Varchar(50) Age Int 2) 顾客旅馆 Id Int customerId Int (Foreign key) customerName Varchar(50) Age Int Loan float Id customerId customerName Age Loan 1 100 John 32 1500 2

我有两张桌子如下 1) 顾客

customerId  Int (primary key)
customerName    Varchar(50)
Age Int
2) 顾客旅馆

Id  Int 
customerId  Int (Foreign key)
customerName    Varchar(50)
Age Int
Loan    float
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 };
            }
        }
    }
从jquery中,我在webservice webmethod中以动态json对象的形式获得多条记录,如下所示(InsertData)。 通过使用IList和实体框架,我插入了多个记录

我在这里的要求是,在插入客户记录时,我想从customer表中删除几个字段,并在customerloan表中删除额外字段

总之,我想插入从customer表生成的cutomerId,CustomerLoan表中只有很少的字段

例:顾客

customerId  customerName    Age
100 John    32
101 Jacob   35
例:CustomerLoan

Id  Int 
customerId  Int (Foreign key)
customerName    Varchar(50)
Age Int
Loan    float
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 };
            }
        }
    }
Id customerId customerName年龄贷款
1100约翰321500
21035 2000
[WebMethod(EnableSession=true)]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共动态InsertData(int-revision、int-appID、dynamicjsonmaster)
{
动态json=jsonMaster;
IList customers=((object[])json)。选择(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();
锁定(_updatePointsLock)
{
使用(CustomerEntities上下文=新CustomerEntities())
{
int currentRevision=context.Customer.Max(x=>x.Revision)??0;
如果(当前修订>=修订版)
{
foreach(客户中的客户cobj)
{
Customer obj=context.Customer.Where(x=>x.customerId==cobj.salessMasterId).FirstOrDefault();
if(obj==null)
{
cobj.版次=当前版次+1;
context.Customer.Add(cobj);
**CustomerLoan objLoan=新CustomerLoan();
objLoan.customerId=cobj.customerId;
objLoan.customerName=cobj.customerName;
objLoan.Age=cobj.Age;
objLoan.customerLoan=1500;
context.CustomerLoan.Add(objLoan)**
}
其他的
{
obj.customerName=cobj.customerName;
目标年龄=目标年龄;
对象版本=当前版本+1;
}
}
SaveChanges();
还新
{
修订版=当前修订版+1,
Customer=context.Customer.Where(x=>x.Revision>Revision)。选择(x=>new
{
x、 客户ID,
x、 客户名称,
x、 年龄,
修订=x.修订,
})托利斯先生()
};
}
其他的
{
返回新的{Revision=Revision};
}
}
}
}

在customerLoan表的customerId字段中插入上述代码(-1)值。 如果创建要插入的对象位于客户未获取的foreach值之外。
如果有人能帮助在customerLoan中插入identity value customer表,我们将不胜感激。

首先使用context.SaveChanges()将您的customer对象保存到数据库中; 然后尝试添加客户贷款(现在您应该能够找到客户Id),并使用context.SaveChanges()再次保存到数据库中


它可能有其他方法,但这是我知道的方法。

当找不到客户并插入新客户时,在调用SaveChanges之前,不会设置自动插入主键customerid,因此它还不能用于外键。。如果我使用context.SaveChanges()将第一个客户对象添加到数据库中;我没有获得客户贷款的客户实例对象(objLoan.customerId=cobj.customerId;)。