我在客户端有一个asp.net mvc Web应用程序,在服务器端有一个OData API。在此客户端应用程序中,我将“ OData客户端”添加到项目中以使用linq查询来调用我的OData API(我可以在vs2017中添加“ OData v4客户端代码生成器”的扩展名后添加OData客户端)。在这个项目中,我有以下查询来调用我的服务器:
string serviceUri = "http://192.168.1.2:1234";
var container = new APIs.Default.Container(new Uri(serviceUri));
var SingleCustomer = container.Customers.Where(n => n.NationalCode == body.NationalCode).Select(x => x).FirstOrDefault();
此后,我用来自“ SingleCustomer”的数据填充ViewModel类,如下所示:
CustomerViewModel customerViewModel = new CustomerViewModel()
{
Address = SingleCustomer.Address,
Adr = SingleCustomer.Adr,
AgentCodeMelli = SingleCustomer.AgentCodeMelli,
BirthDay = SingleCustomer.BirthDay,
BirthMonth = SingleCustomer.BirthMonth,
BirthPlace = SingleCustomer.BirthPlace,
BirthYear = SingleCustomer.BirthYear,
BusinessGroup = SingleCustomer.BusinessGroup,
CIIValidationStatus = SingleCustomer.CIIValidationStatus
}
之后,我将ViewModel类中的数据添加到本地数据库中,如下所示:
SeeMiddleEntities db = new SeeMiddleEntities();
APIs.Models.Customer customer = new APIs.Models.Customer()
{
Address = SingleCustomer.Address,
Adr = SingleCustomer.Adr,
AgentCodeMelli = SingleCustomer.AgentCodeMelli,
BirthDay = SingleCustomer.BirthDay,
BirthMonth = SingleCustomer.BirthMonth,
BirthPlace = SingleCustomer.BirthPlace,
BirthYear = SingleCustomer.BirthYear,
BusinessGroup = SingleCustomer.BusinessGroup,
CIIValidationStatus = SingleCustomer.CIIValidationStatus
};
if (SingleCustomer != null)
{
try
{
db.Customer.Add(customer);
db.SaveChanges();
}
catch (Exception e)
{
Error res = new Error
{
_Error = "The customer is not inserted",
Code = 417,
};
return ResponseMessage(Request.CreateResponse<Error>(HttpStatusCode.ExpectationFailed, res));
}
List<BN> listBns = new List<BN>();
var CustomerId = SingleCustomer.Id;
var BN = container.BNs.Where(n => n.BimeGozarCustomerId == CustomerId && n.ReshteIsLife == true).Select(x => x).ToList();
foreach (var items in BN)
{
BN bn = new BN()
{
Id = items.Id,
AgentLocationBeginDate = items.AgentLocationBeginDate,
AgentLocationBranchCode = items.AgentLocationBranchCode,
AgentLocationBranchDisplayName = items.AgentLocationBranchDisplayName,
AgentLocationCityName = items.AgentLocationCityName,
AgentLocationCode = items.AgentLocationCode,
AgentLocationDegree = items.AgentLocationDegree,
AgentLocationDisplayName = items.AgentLocationDisplayName,
AgentLocationID = items.AgentLocationID,
AgentLocationKindName = items.AgentLocationKindName,
AgentLocationMapCode = items.AgentLocationMapCode,
AgentLocationPersonKindName = items.AgentLocationPersonKindName,
AgentLocationProvinceName = items.AgentLocationProvinceName
};
listBns.Add(bn);
}
if (BN.Count != 0)
{
try
{
foreach (var Bn in listBns)
{
db.BN.Add(Bn);
db.SaveChanges();
}
}
catch (Exception e)
{
Error res = new Error
{
_Error = "There is not",
Code = 417,
};
return ResponseMessage(Request.CreateResponse<Error>(HttpStatusCode.ExpectationFailed, res));
}
}
我进行了搜索,有人说这个问题可以使用Code First来解决,也可以通过DotNet Core来解决,但是我对此一无所知,没有时间去学习。如何使用Asp.Net和Entity Framework 6.0解决此问题???????