I have two tables as follows 1) Customers
customerId Int (primary key)
customerName Varchar(50)
Age Int
2) CustomerLoan
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). By using IList and Entity framework I am inserting multiple records.
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.
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. If create objects to insert outside the foreach values of Customers not getting. If someone can help inserting identity value customer table in customerLoan highly appreciated.
First save your customer object to database with 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();
It may have other ways to do it but this is the way I know.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.