EF手动加载 /填充导航属性
我有以下代码从服务获取数据并将其附加到EntityFramework 6从本地数据库中加载的对象!
我们之前在本地数据库中有UserJob的详细信息,但是我们决定以后将其移至服务,并将所有用户job迁移到新的微服务。
我们处于此举中间,现在我们将用户作业保存在本地数据库和新服务中!
当我们从Micro Service阅读并更新本地数据库时,该代码将在新服务中不再可用,因为JobID不再可用,但是我们有相同的创建日期和用户ID来查找本地数据库中可用的JobID呢我现在正确设置了它。
private void IncludeUserJobOnUser(UserJob userJob, MyDBContext db)
{
var aspnetUser = new EFDataReader().GetUser(userJob.userId, db);
if (userJob != null && aspnetUser != null)
{
userJob.JobId = new EFDataReader().GetUserJobId(userJob.userId, userJob.CreationDate, db);
DbEntityEntry entry = db.Entry(userJob);
entry.State = EntityState.Unchanged;
if (aspnetUser.UserJobs.Any(x => x.JobId != userJob.JobId)) // Here is the problem
{
aspnetUser.UserJobs.Add(userJob);
}
userJob.AspNetUser = aspnetUser;
}
}
问题:
我试图用从微服务中检索的对象设置导航属性。但是,EF6始终列入数据库,并从本地数据库中填充它们,而是添加实体来自微服务。
有什么想法如何解决上述?
I have the below code that fetches the data from service and attaches to the objects that was loaded from local database by EntityFramework 6!
We had the UserJob details before in our local database, However we decided to move it to the service later and migrated all the UserJobs to new micro service.
We are in the middle of this move and now we are saving the User jobs in both local database and in the new service!
When we read from micro service and update the local database as well, the code treats its a new job, since the JobId is no longer available in new service, but we have same creation date and user id to find the JobId available in local database! I set it now correctly.
private void IncludeUserJobOnUser(UserJob userJob, MyDBContext db)
{
var aspnetUser = new EFDataReader().GetUser(userJob.userId, db);
if (userJob != null && aspnetUser != null)
{
userJob.JobId = new EFDataReader().GetUserJobId(userJob.userId, userJob.CreationDate, db);
DbEntityEntry entry = db.Entry(userJob);
entry.State = EntityState.Unchanged;
if (aspnetUser.UserJobs.Any(x => x.JobId != userJob.JobId)) // Here is the problem
{
aspnetUser.UserJobs.Add(userJob);
}
userJob.AspNetUser = aspnetUser;
}
}
PROBLEM:
I am trying to set the navigational property with the objects retrieved from the microservice. However, EF6 always hits the database and populate them from local database instead adding the entity came from microservice.
Any idea how to solve the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的意思是访问“ aspnetuser.userjobs”,您会看到一个查询以加载用户job作为
any()any()
检查的一部分,这将是懒惰的加载。如果当数据阅读器加载用户时,用户job已经急切地加载了,那么这将不会发生。如果用户尚未加载作业,代码将如何知道用户是否有该作业?如果不小心,这样的代码在运行时可能很危险,因为您将实体参考传递给了其他可能或可能仍未在跟踪这些实体的dbContext范围关联的实体。前提是“ db” dbContext实例是用于检索所有实体(包括用户job)的单个总体dbcontext(包括用户job)或关联到那样,但是当我阅读诸如从微服务中检索到的对象诸如发送点的诸如“作为微服务的危险信号将在范围内范围范围,因此当前的dbcontext返回实体不会知道其与此类结果相关联的实体。
If you mean that by accessing "aspnetUser.UserJobs" you are seeing a query kicked off to load the UserJobs as part of the
Any()
check, that would be Lazy Loading. If the UserJobs were already eager loaded when the data reader loaded the user then this wouldn't happen. If the user hasn't loaded the jobs, how would the code know if the user had that job or not?Code like this can be dangerous at runtime if not careful because you are passing entity references around to be associated with other entities where the scope of the DbContext that may, or may not still be tracking these entities is not clearly defined. Provided that "db" DbContext instance is the single overarching DbContext for retrieving all entities (including UserJob) are loaded from or associated to then you should be ok, but when I read things like "objects retrieved from the microservice" that sends up a bit of a red flag as Microservices would be scoping their own DbContext so returning entities from one would not be known by the current DbContext whose entities you are associating such results with.