单个对象层次结构,EF中的多个备份数据库

发布于 2025-02-03 01:36:59 字数 1142 浏览 1 评论 0原文

我有以下类:(

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解析到实际用户对象中。选项似乎是:

  1. 手动修复模型层次结构,通过将其穿过并加载缺失用户用户属性立即在每个EF查询到Moderndb 。

  2. 使用户用户属性懒惰,并在首次访问时解决。

两种选项似乎都不错:选项(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:

  1. Manually fixup the Model hierarchy by traversing it and loading missing User User properties immediately after each EF query to ModernDB.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文