单个对象层次结构,EF中的多个备份数据库
我有以下类:(
class User
{
public int Id { get; set; }
string Email { get; set; }
}
class Model1
{
Model2 Model2 { get; set }
Model3 Model3 { get; set }
User User { get; set; }
}
class Model2
{
User User2 { get; set; }
}
class Model3
{
User User3 { get; set; }
}
实际上,模型层次结构更深,还有许多其他属性。重要的是,用户类是在模型层次结构的几个不同位置引用的。)
我正在使用此类层次结构映射此类层次结构 。 EF。我被迫使用两个不同的关系数据库,这是一个已经映射用户类的旧数据库,但我否则我无法触摸。和一个在我控制的“模型”。同样,我可以更改模型[123]
,但不能更改用户
class(ES)。使用模型[123]
必须访问用户用户
属性的客户端代码。
EF不支持数据库间的连接。我尝试模仿一个,这样:我添加了int userid
用户用户
字段在所有模型[123]
中,然后标记为<代码>用户用户 as [notMapped]
。问题是何时将这些ID解析到实际用户
对象中。选项似乎是:
手动修复
模型
层次结构,通过将其穿过并加载缺失用户用户
属性立即在每个EF查询到Moderndb 。
使
用户用户
属性懒惰,并在首次访问时解决。
两种选项似乎都不错:选项(1)是错误的和重复的;选项(2)在客户端代码应该能够假定的呼叫中引入了异步操作。 (另外,我尝试尝试时似乎很容易引入僵局。)
EF是否有一个钩子,可以在填充EF实体时运行异步代码?
是否有一个好方法模仿数据库中的EF中加入?
I have these classes:
class User
{
public int Id { get; set; }
string Email { get; set; }
}
class Model1
{
Model2 Model2 { get; set }
Model3 Model3 { get; set }
User User { get; set; }
}
class Model2
{
User User2 { get; set; }
}
class Model3
{
User User3 { get; set; }
}
(In practice the Model hierarchy is deeper and has many other properties. The important thing is that the User class is referenced in several different places of the Model hierarchy.)
I am in the process of mapping this class hierarchy using EF. I am forced to use two distinct relational databases, a legacy "UserDB" which already maps the User class, but which I otherwise cannot touch; and a "ModelDB" which is under my control. Similarly, I can change the Model[123]
but not the User
class(es). Client code using Model[123]
must have access to a User User
property.
EF does not support inter-database joins. I tried to emulate one, like this: I added a int UserId
field accompanying User User
to all of Model[123]
, then marked User User
as [NotMapped]
. The question is when to resolve these ids into actual User
objects. The options seem to be:
Manually fixup the
Model
hierarchy by traversing it and loading missingUser User
properties immediately after each EF query toModernDB
.Make the
User User
properties lazy, and have them resolve upon first access.
Neither option seems good: Option (1) is error prone and repetitive; Option (2) introduces what should be asynchronous operations in calls that client code should be able to assume is asynchronous. (Also, I seemed to very easily introduce deadlocks when I tried.)
Does EF have a hook that allows me to run asynchronous code when an EF-entity is populated?
Is there a good way to emulate inter-database joins in EF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论