实体框架Post方法
本文关键字:方法 Post 框架 实体 | 更新日期: 2023-09-27 18:07:57
我有两个表(Testresult &SampleCollection)在数据库中相互链接。我在实体框架中创建了一个Post控制器,同时将数据保存到两个表。但是,在将数据保存到两个表时,应该应用一个特殊的条件。我有条件A,条件B,条件C和条件D:
条件A是如下截图:
[
{
"TestResult": {
"iOutletStorageId": 50061,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 1: Mark Cert",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 1: First Result Remarks",
"szSecondResultRemark": "Tank 1: Second Result Remarks 2",
"szSiteVisitNo": "SVN01",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
},
"MySampleCollections": {
"iPPMSTestResultId": 0,
"szZonalLabelNumber": "Tank 1: Sample Lbl No Zonal",
"szZonalSampleQty": "275",
"szZonalSerialNumber": "Tank 1: Seal No. Zonal",
"szCLabLabelNumber": "Tank 1: Sample Lbl No C-Lab",
"szCLabSampleQty": "275",
"szCLabSerialNumber": "Tank 1: Seal No. C-Lab",
"szGSALabelNumber": "Tank 1: Sample Lbl No GSA",
"szGSASampleQty": "275",
"szGSASerialNumber": "Tank 1: Seal No. GSA",
"szOutletLabelNumber": "Tank 1: Sample Lbl No Outlet",
"szOutletSampleQty": "275",
"szOutletSerialNumber": "Tank 1: Seal No. Outlet",
"szOutletSampleRecievedBy": "Tank 1: Sample Received By Outlet",
"szRetentionLabelNumber": "Tank 1: Sample Lbl No Retention",
"szRetentionSampleQty": "275",
"szRetentionSerialNumber": "Tank 1: Seal No. Retention"
}
}
]
条件B是如下截图:
[
{
"TestResult": {
"iOutletStorageId": 50062,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 2: Cert #",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 2: First test Remarks",
"szSecondResultRemark": "Tank 2: Second Test Remarks",
"szSiteVisitNo": "SVN02",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
},
"MySampleCollections": {
"iPPMSTestResultId": 0,
"szZonalLabelNumber": "Tank 1: Sample Lbl No Zonal",
"szZonalSampleQty": "275",
"szZonalSerialNumber": "Tank 1: Seal No. Zonal",
"szCLabLabelNumber": "Tank 1: Sample Lbl No C-Lab",
"szCLabSampleQty": "275",
"szCLabSerialNumber": "Tank 1: Seal No. C-Lab",
"szGSALabelNumber": "Tank 1: Sample Lbl No GSA",
"szGSASampleQty": "275",
"szGSASerialNumber": "Tank 1: Seal No. GSA",
"szOutletLabelNumber": "Tank 1: Sample Lbl No Outlet",
"szOutletSampleQty": "275",
"szOutletSerialNumber": "Tank 1: Seal No. Outlet",
"szOutletSampleRecievedBy": "Tank 1: Sample Received By Outlet",
"szRetentionLabelNumber": "Tank 1: Sample Lbl No Retention",
"szRetentionSampleQty": "275",
"szRetentionSerialNumber": "Tank 1: Seal No. Retention"
}
}
]
条件C是如下截图:
[
{
"TestResult": {
"iOutletStorageId": 50063,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 2: Cert #",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 2: First test Remarks",
"szSecondResultRemark": "Tank 2: Second Test Remarks",
"szSiteVisitNo": "SVN02",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
},
"MySampleCollections": {
"iPPMSTestResultId": 0,
"szZonalLabelNumber": "",
"szZonalSampleQty": "",
"szZonalSerialNumber": "",
"szCLabLabelNumber": "",
"szCLabSampleQty": "",
"szCLabSerialNumber": "",
"szGSALabelNumber": "",
"szGSASampleQty": "",
"szGSASerialNumber": "",
"szOutletLabelNumber": "",
"szOutletSampleQty": "",
"szOutletSerialNumber": "",
"szOutletSampleRecievedBy": "",
"szRetentionLabelNumber": "",
"szRetentionSampleQty": "",
"szRetentionSerialNumber": ""
}
}
]
条件D是如下截图:
[
{
"TestResult": {
"iOutletStorageId": 50063,
"dLastDeliveryDate": "2016-9-14",
"szMarkingCertificate": "Tank 2: Cert #",
"iFirstResultId": "1",
"iSecondResultId": "2",
"szFirstResultRemark": "Tank 2: First test Remarks",
"szSecondResultRemark": "Tank 2: Second Test Remarks",
"szSiteVisitNo": "SVN02",
"dTransdate": "2016-9-20 10:4:10.754",
"iOutletId": 756
}
}
]
现在我想一次保存两行或多行。
当我通过条件A &B、它直接保存到数据库当我通过条件A时;C,它保存到数据库当我通过条件A时;D,我得到一个错误。
这个想法是,第二个表将从第一个表获得它的ID。
下面是我的代码:
public int PostTestResultObj(List<TestAndSampleResult> testandsampleresult)
{
long testResultId = 0;
if (testandsampleresult != null)
foreach (TestAndSampleResult orderdet in testandsampleresult)
{
mdlTestResult NewTestResult = new mdlTestResult
{
iOutletStorageId = orderdet.TestResult.iOutletStorageId,
dLastDeliveryDate = orderdet.TestResult.dLastDeliveryDate,
szMarkingCertificate = orderdet.TestResult.szMarkingCertificate,
iFirstResultId = orderdet.TestResult.iFirstResultId,
iSecondResultId = orderdet.TestResult.iSecondResultId,
szFirstResultRemark = orderdet.TestResult.szFirstResultRemark,
szSecondResultRemark = orderdet.TestResult.szSecondResultRemark,
szSiteVisitNo = orderdet.TestResult.szSiteVisitNo,
dTransdate = orderdet.TestResult.dTransdate,
iOutletId = orderdet.TestResult.iOutletId
};
SampleCollections newsamplecollectionmodel = new SampleCollections
{
szZonalLabelNumber = orderdet.MySampleCollections.szZonalLabelNumber,
szZonalSampleQty = orderdet.MySampleCollections.szZonalSampleQty,
szZonalSerialNumber = orderdet.MySampleCollections.szZonalSerialNumber,
szCLabLabelNumber = orderdet.MySampleCollections.szCLabLabelNumber,
szCLabSampleQty = orderdet.MySampleCollections.szCLabSampleQty,
szCLabSerialNumber = orderdet.MySampleCollections.szCLabSerialNumber,
szGSALabelNumber = orderdet.MySampleCollections.szGSALabelNumber,
szGSASampleQty = orderdet.MySampleCollections.szGSASampleQty,
szGSASerialNumber = orderdet.MySampleCollections.szGSASerialNumber,
szOutletLabelNumber = orderdet.MySampleCollections.szOutletLabelNumber,
szOutletSampleQty = orderdet.MySampleCollections.szOutletSampleQty,
szOutletSerialNumber = orderdet.MySampleCollections.szOutletSerialNumber,
szOutletSampleRecievedBy = orderdet.MySampleCollections.szOutletSampleRecievedBy,
szRetentionLabelNumber = orderdet.MySampleCollections.szRetentionLabelNumber,
szRetentionSampleQty = orderdet.MySampleCollections.szRetentionSampleQty,
szRetentionSerialNumber = orderdet.MySampleCollections.szRetentionSerialNumber
};
DataTable mydtTransReslt = Uow.tresult.GetTestResultNo(NewTestResult.iOutletStorageId,
NewTestResult.dLastDeliveryDate, NewTestResult.szMarkingCertificate,
NewTestResult.iFirstResultId, NewTestResult.iSecondResultId,
NewTestResult.szFirstResultRemark, NewTestResult.szSecondResultRemark,
NewTestResult.szSiteVisitNo, NewTestResult.dTransdate, NewTestResult.iOutletId);
if (mydtTransReslt.Rows.Count > 0)
{
testResultId = Convert.ToInt32(mydtTransReslt.Rows[0]["NewPPMSTestResult"]);
}
if (testResultId > 0)
{
long samples = Uow.spleCollections.GetSampleCollectionsNo(testResultId,
newsamplecollectionmodel.szZonalLabelNumber,
newsamplecollectionmodel.szZonalSampleQty,
newsamplecollectionmodel.szZonalSerialNumber,
newsamplecollectionmodel.szCLabLabelNumber,
newsamplecollectionmodel.szCLabSampleQty,
newsamplecollectionmodel.szCLabSerialNumber,
newsamplecollectionmodel.szGSALabelNumber,
newsamplecollectionmodel.szGSASampleQty,
newsamplecollectionmodel.szGSASerialNumber,
newsamplecollectionmodel.szOutletLabelNumber,
newsamplecollectionmodel.szOutletSampleQty,
newsamplecollectionmodel.szOutletSerialNumber,
newsamplecollectionmodel.szOutletSampleRecievedBy,
DateTime.UtcNow,
newsamplecollectionmodel.szRetentionLabelNumber,
newsamplecollectionmodel.szRetentionSampleQty,
newsamplecollectionmodel.szRetentionSerialNumber);
}
else
{
mydtTransReslt.Columns.Add("Exception", typeof (string));
mydtTransReslt.Rows.Add("Sorry!!! Transaction Failed");
}
}
return 1;
}
我是否有可能同时通过条件A &D,没有任何误差。如果可能,请告诉我怎么做?谢谢,Somad
我通过输入以下代码来解决我的问题:
public int PostTestResultObj(List<TestAndSampleResult> testandsampleresult)
{
long testResultId = 0;
if (testandsampleresult != null)
foreach (TestAndSampleResult orderdet in testandsampleresult)
{
if (orderdet.TestResult != null) //Making sure our results object is passing null
{
mdlTestResult NewTestResult = new mdlTestResult
{
iOutletStorageId = orderdet.TestResult.iOutletStorageId,
dLastDeliveryDate = orderdet.TestResult.dLastDeliveryDate,
szMarkingCertificate = orderdet.TestResult.szMarkingCertificate,
iFirstResultId = orderdet.TestResult.iFirstResultId,
iSecondResultId = orderdet.TestResult.iSecondResultId,
szFirstResultRemark = orderdet.TestResult.szFirstResultRemark,
szSecondResultRemark = orderdet.TestResult.szSecondResultRemark,
szSiteVisitNo = orderdet.TestResult.szSiteVisitNo,
dTransdate = orderdet.TestResult.dTransdate,
iOutletId = orderdet.TestResult.iOutletId
};
//Persist the result
DataTable mydtTransReslt = Uow.tresult.GetTestResultNo(NewTestResult.iOutletStorageId,
NewTestResult.dLastDeliveryDate,
NewTestResult.szMarkingCertificate,
NewTestResult.iFirstResultId,
NewTestResult.iSecondResultId,
NewTestResult.szFirstResultRemark,
NewTestResult.szSecondResultRemark,
NewTestResult.szSiteVisitNo,
NewTestResult.dTransdate,
NewTestResult.iOutletId);
testResultId = 0;
if (mydtTransReslt.Rows.Count > 0) //Result inserted successfully
{
//Get the id of new result
testResultId = Convert.ToInt32(mydtTransReslt.Rows[0]["NewPPMSTestResult"]);
if (testResultId > 0)//Result insert was successful
{
if (orderdet.MySampleCollections != null && testResultId > 0) //Samples have been passed from client
{
foreach (SampleCollection Sample in orderdet.MySampleCollections)
{
long samples = Uow.spleCollections.GetSampleCollectionsNo(testResultId,
//newsamplecollectionmodel.iPPMSTestResultId,
Sample.iSampleTypeId,
Sample.szLabelNumber,
Sample.iSampleQty,
Sample.szSerialNumber,
Sample.szRecievedBy);
}
}
}
else
{
mydtTransReslt.Columns.Add("Exception", typeof(string));
mydtTransReslt.Rows.Add("Sorry!!! Transaction Failed");
}
}
}
}
return 1;
}
它完全按照我想要的方式保存…由于