我正在尝试保存到两个不同的表中。从Taxlaw中获取数据并保存时,我将相同数据保存在taxlawhistory表中。因此,就像将数据从一个表复制到另一表一样,这是行不通的。我不断收到的错误是
无法从Model.TaxLaw转换为Model.TaxTableHisory
public async Task<bool> SaveOrUpdate(IEnumerable<TaxLaw> obj)
{
using (var trans = _taxhistoryrepo.DbContext.Database.BeginTransaction())
{
var lists = new List<TaxTableHistory>();
foreach (var list in obj)
{
lists.CompanyCode = list.CompanyCode;
lists.CummulativeAmount = list.CummulativeAmount;
lists.Percentage = list.Percentage;
lists.ModifiedDate = DateTime.UtcNow;
lists.ModifiedDate = list.ModifiedDate;
lists.Status = EntityStatus.Active;
_taxhistoryrepo.DbSetEntity.Add(lists);
await _taxhistoryrepo.DbContext.SaveChangesAsync();
await Task.FromResult(_repository.SingleSave(obj));
}
trans.Commit();
}
return true;
}
答案 0 :(得分:2)
尝试:
public async Task<bool> SaveOrUpdate(IEnumerable<TaxLaw> obj)
{
using (var trans = _taxhistoryrepo.DbContext.Database.BeginTransaction())
{
// var lists = new List<TaxTableHistory>();
foreach (var taxLaw in obj)
{
var taxTableHistory = new TaxTableHistory();
taxTableHistory.CompanyCode = taxLaw.CompanyCode;
taxTableHistory.CummulativeAmount = taxLaw.CummulativeAmount;
taxTableHistory.Percentage = taxLaw.Percentage;
taxTableHistory.ModifiedDate = DateTime.UtcNow;
taxTableHistory.ModifiedDate = taxLaw.ModifiedDate;
taxTableHistory.Status = EntityStatus.Active;
_taxhistoryrepo.DbSetEntity.Add(taxTableHistory);
await _taxhistoryrepo.DbContext.SaveChangesAsync();
await Task.FromResult(_repository.SingleSave(obj));
}
trans.Commit();
}
return true;
}
答案 1 :(得分:2)
obj的类型
public class DiffPackageSubClass extends Test {
public static void main(String[] args) {
Test t1 = new Test();
System.out.println(t1.a);
t1.display(); // error
DiffPackageSubClass diffPackageSubClass = new DiffPackageSubClass();
diffPackageSubClass.display(); // ok
}
}
所以每个团队都是
IEnumerable<TaxLaw>
然后您要保存到
TaxLaw
我相信正在使用
_taxhistoryrepo
这部分代码:
TaxHistory
通过obj进行描述,因此“列表”是TaxLaw,但在这里:
foreach (var list in obj)
{
list.CompanyCode = list.CompanyCode;
list.CummulativeAmount = list.CummulativeAmount;
list.Percentage = list.Percentage;
list.ModifiedDate = DateTime.UtcNow;
list.ModifiedDate = list.ModifiedDate;
list.Status = EntityStatus.Active;
_taxhistoryrepo.DbSetEntity.Add(list);
await _taxhistoryrepo.DbContext.SaveChangesAsync();
await Task.FromResult(_repository.SingleSave(obj));
}
您将TaxLaw类型添加到TaxHistory表
我也相信这里是您的错字
_taxhistoryrepo.DbContext.SaveChangesAsync()
基本上是用相同的变量设置相同的变量
我想你想要实现的是:
list.CompanyCode = list.CompanyCode;
...
答案 2 :(得分:2)
您试图将Model.TaxLaw对象添加到TaxTableHisory存储库中,因为obj是TaxLaw的IEnumerable。
foreach (var list in obj)
_taxhistoryrepo.DbSetEntity.Add(list);
您需要创建一个类型为TaxTableHisory的新对象并添加该对象
public async Task<bool> SaveOrUpdate(IEnumerable<TaxLaw> taxLawList)
{
using (var trans = _taxhistoryrepo.DbContext.Database.BeginTransaction())
{
foreach (var taxLaw in taxLawList)
{
var taxTableHisory = new TaxTableHisory();
taxTableHisory.CompanyCode = taxLaw.CompanyCode;
taxTableHisory.CummulativeAmount = taxLaw.CummulativeAmount;
taxTableHisory.Percentage = taxLaw.Percentage;
taxTableHisory.ModifiedDate = taxLaw.ModifiedDate;
taxTableHisory.Status = EntityStatus.Active;
_taxhistoryrepo.DbSetEntity.Add(taxTableHisory);
await _taxhistoryrepo.DbContext.SaveChangesAsync();
}
trans.Commit();
}
return true;
}
Ps:可能taxTableHisory是taxTableHistory的类型错误
答案 3 :(得分:0)
尽管以下代码可以解决您的问题,但是您可以使用AutoMapper将一个对象映射到另一个对象。
public async Task<bool> SaveOrUpdate(IEnumerable<TaxLaw> obj)
{
using (var trans = _taxhistoryrepo.DbContext.Database.BeginTransaction())
{
foreach (var list in obj)
{
_taxhistoryrepo.DbSetEntity.Add(new TaxTableHistory {
CompanyCode = list.CompanyCode,
CummulativeAmount = list.CummulativeAmount,
Percentage = list.Percentage,
ModifiedDate = DateTime.UtcNow,
ModifiedDate = list.ModifiedDate,
Status = EntityStatus.Active
});
await _taxhistoryrepo.DbContext.SaveChangesAsync();
await Task.FromResult(_repository.SingleSave(obj));
}
trans.Commit();
}
return true;
}