如何修改其他程序集中已映射实体的映射?

发布于 2025-01-07 02:01:51 字数 583 浏览 0 评论 0原文

我想为 EF 中的实体自定义映射

例如,我有一个实体:

public class User
{  
    public int UserId { get;set;}
    public string Firstname { get;set;}
    public string ColA {get; set;}
    public string ColB {get; set;}
}

它已经在常用的 EF 中的 OnModelCreating() 中添加了映射。如果我在 2 个独立的程序集中有 2 个函数,请调用 AssemblyA.dll 和 AssemblyB.dll,它们由 MEF 框架动态加载。

AssemblyA.dll 只需要 ColA,并且必须删除 ColB,反之亦然,对于 AssemblyB.dll,因此我需要为这 2 个服务定义 2 个新的映射类,并且它将根据其程序集动态运行以删除列(AssemblyA 将忽略 ColB,且 AssemblyB 忽略 ColA)。我不想修改当前 EF 的代码,因为它已经在生产中了。每个程序集的所有更改都应单独进行。

EF支持我们这样做吗?或者你能给我一个方向吗?

I'd like to custom mapping for entities in EF

For example, I have an entity:

public class User
{  
    public int UserId { get;set;}
    public string Firstname { get;set;}
    public string ColA {get; set;}
    public string ColB {get; set;}
}

It's already added mapping in OnModelCreating() in EF in common use. If i have 2 functions in 2 seperate assemblies, call AssemblyA.dll and AssemblyB.dll and they are dynamically loaded by MEF framework.

AssemblyA.dll just needs ColA and has to remove ColB and vice versa for AssemblyB.dll, so i need to define 2 new mapping classes for these 2 services and it will dynamically run to remove column according to its assembly (AssemblyA will Ignore ColB, and AssemblyB ignore ColA). I don't want to modify code of current EF because it's already in production. All changes for each assembly should be in its own.

Do EF support us to do like this ? Or could you give me a direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的奇迹 2025-01-14 02:01:51

目前还不清楚您要做什么,但是:

  • 每个 EF 上下文只能将每个表和实体映射一次
  • 这意味着如果您加载 AssemblyA 的配置,则无法使用 AssemblyB 的配置
  • 这也意味着您无法使用 EF 映射的默认方式是在 OnModelCreating 内部构造的,因为该方法通常在整个应用程序生命周期中仅调用一次。
  • 您可以手动构造两个 DbModel 实例,将它们编译为 DbCompiledModel 并将它们传递给 DbContext 构造函数 - 这应该允许您拥有两种不同的映射配置对于 AssemblyA 和 AssemblyB,但您永远不会在同一个上下文实例中拥有它们。
  • EF 迁移很可能不起作用,因为它们期望每个数据库有一个映射集

无论如何,如果您使用 MEF 和模块化架构,每个实体应该是核心(不与任何特定模块相关并在模块之间共享)或模块(不是被任何其他模块或核心使用)。

It is not very clear what you are trying to do but:

  • Every EF context can have each table and entity mapped only once
  • It means if you load configuration for AssemblyA you cannot use configuration for AssemblyB
  • It also means you cannot use default way how EF mapping is constructed inside OnModelCreating because that method is normally called only once for whole application lifetime.
  • You can manually construct two DbModel insnaces, compile them to DbCompiledModel and pass them to DbContext constructor - that should allow you to have two different mapping configuration for AssemblyA and AssemblyB but you will never have both of them in the same context instance.
  • EF migrations will most probably don't work because they expect single mapping set per database

Anyway if you are using MEF and modular architecture each entity should be either core (not related to any particular module and shared as is among modules) or module (not used by any other modules or core).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文