MEF 并在运行时加载 EntityTypeConfiguration
您不能对自己的帖子进行投票 0
嗨。
我正在开发这个(http://arg-co.com/SabteNam%20-%20Copy.zip)Windows 应用程序,对于我的 DAL,我使用实体框架。但每个扩展都有自己的 EntityTypeConfiguration,因此我决定使用 [Import] 和 [Export] 将它们添加到我的 DbContext 的 OnModelCreating 方法中。这里的问题是,在位于“SabteNamDataAccess”库上的“SabteNamDbContext”类中,“_Configs”未初始化,因此我无法迭代它并将其项目添加到“modelBuilder.Configurations”。
在“SampleConfiguration”类的源代码中,我注释掉了“[Export(typeof(IDbConfiguration))]”,但即使取消注释这部分代码,也不会导致应用程序正常工作。
有趣的是,如果我在“主”窗口窗体中使用以下代码,“_Configs”将被初始化:
[ImportMany(typeof(IDbConfiguration))]
public IEnumerable<EntityTypeConfiguration<object>> _Configs { get; set; }
如何修复此问题?
You cannot vote on your own post
0
Hi.
I am developing this (http://arg-co.com/SabteNam%20-%20Copy.zip) windows application, and for my DAL I use Entity Framework. But every single extension has its own EntityTypeConfiguration, so I decided to use [Import] and [Export] to add them in OnModelCreating method of my DbContext.The problem here is that, in 'SabteNamDbContext' class which is located on 'SabteNamDataAccess' library, the '_Configs' is not initialized so I cant iterate it and add its items to 'modelBuilder.Configurations'.
In the source code of 'SampleConfiguration' class, I commented out '[Export(typeof(IDbConfiguration))]' but even Uncommenting this part of code, do not cause application to work properly.
Intresting point is that, if I use the following code in 'Main' windows form, the '_Configs' would be initialized :
[ImportMany(typeof(IDbConfiguration))]
public IEnumerable<EntityTypeConfiguration<object>> _Configs { get; set; }
How can this be fixed ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我意识到这可能不再对您有用,但我们使用 此模型来自 OdeToCode,我建议您阅读。
在我们的例子中,我们通常为我们的扩展创建了一个接口,而不仅仅是像 Scott 那样为实体配置创建了一个接口,这使我们不仅可以加载配置,还可以为每个扩展添加工厂和测试数据,向核心应用程序等。
我们的 OnModelCreating 看起来像这样:
While I realize this is probably no longer of use to you, we use a variation on this model from OdeToCode, which I advise you read.
In our case, we have created an interface for our extensions in general, not just for the entity configuration like Scott did, which allows us not only to load the configurations, but also factory and test data per extension, add new permission types to the core application, etc.
Our OnModelCreating looks something like this: