如何修改其他程序集中已映射实体的映射?
我想为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前还不清楚您要做什么,但是:
无论如何,如果您使用 MEF 和模块化架构,每个实体应该是核心(不与任何特定模块相关并在模块之间共享)或模块(不是被任何其他模块或核心使用)。
It is not very clear what you are trying to do but:
OnModelCreating
because that method is normally called only once for whole application lifetime.DbModel
insnaces, compile them toDbCompiledModel
and pass them toDbContext
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.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).