C#实体框架在提交后未更新或刷新(保存更改)
我们在系统中发现了一个错误。我们使用的是实体框架6。似乎EF在提交(saveChanges())后没有更新或重新引用 过程: 将项添加到EntityFramework:C#实体框架在提交后未更新或刷新(保存更改),c#,entity-framework-6,C#,Entity Framework 6,我们在系统中发现了一个错误。我们使用的是实体框架6。似乎EF在提交(saveChanges())后没有更新或重新引用 过程: 将项添加到EntityFramework: ShareRefund e = new ShareRefund() { Client_id = message.Client_id, SubcriptionCertificate_id = suct.Id, DTAUS = message.D
ShareRefund e = new ShareRefund()
{
Client_id = message.Client_id,
SubcriptionCertificate_id = suct.Id,
DTAUS = message.DTAUS,
Accounting_id = deposit.Id,
Amount = maxRefund,
Created_id = message.Created_id,
Employee = _context.Employees.FirstOrDefault(p=> p.Id == message.Employee_id),
SubscriptionCertificate = suct,
Accounting = deposit,
Client = _context.Clients.FirstOrDefault(p => p.Id == message.Client_id),
Login = _context.Logins.FirstOrDefault(p => p.Id == message.Created_id),
CreatedDate = DateTime.Now,
CustomId = suct.CustomId,
Employee_id = message.Employee_id,
NoInterest = message.NoInterest,
RefundDate = message.RefundDate
};
_context.ShareRefunds.Add(e);
在此之后,我们调用\u context.Commit()代码>
其定义如下:
public void Commit() {
try {
this.SaveChanges();
}
catch () {
some code here
}
}
在完成该方法之前,我们进入另一个类并使用\u context
的新实例。
在这里,我们希望通过以下方式获得添加的项目e
:
int refundedId = shareRefundId.First();
ShareRefund refund = context.ShareRefunds
.Where(t => t.Client_id == client.Id)
.FirstOrDefault(t => t.Id == refundedId);
此时,当我使用调试器进入数据库并查看数据库时,数据库中存在具有给定id的条目。但是\u上下文
总是返回null
提交后是否有任何已知问题或解决方案可以正确刷新上下文
这是我的构造函数,其中创建了“context”的新实例:
private readonly Context context;
#region constructor/destructor
public ReportingManager() {
context = new Context("Context");
workingDirectory = String.Format(workingDirectory, InstanceConfiguration.DefaultAppFolder);
configDirectory = workingDirectory + @"configs\";
templateDirectory = workingDirectory + @"templates\";
temporaryDirectory = workingDirectory + @"temp\";
}
public ReportingManager(string connectionString, string appFolder) {
context = new Context(connectionString);
workingDirectory = String.Format(workingDirectory, appFolder);
configDirectory = workingDirectory + @"configs\";
templateDirectory = workingDirectory + @"templates\";
temporaryDirectory = workingDirectory + @"temp\";
}
请在post中编写您的_上下文实例定义代码。示例:DbEntitiesName _context=new DbEntitiesName();通常,一个空的、吃异常的catch
语句会使编写稳定的代码变得困难/不可能。我想看一个例子,在这个例子中,我可以通过读取代码来跟踪所有值。您是否忘记将Id放入共享退款Id
?您使用的是同一个客户端id吗?其中有一些处理异常的代码。调用“SaveChanges()”时,在catch-block中捕捉不到任何内容。不,完全正确。id存在,客户端id为,请在post上下文实例定义代码中编写。示例:DbEntitiesName _context=new DbEntitiesName();通常,一个空的、吃异常的catch
语句会使编写稳定的代码变得困难/不可能。我想看一个例子,在这个例子中,我可以通过读取代码来跟踪所有值。您是否忘记将Id放入共享退款Id
?您使用的是同一个客户端id吗?其中有一些处理异常的代码。调用“SaveChanges()”时,在catch-block中捕捉不到任何内容。不,完全正确。id存在,客户端id也存在