MEF 并在运行时加载 EntityTypeConfiguration

发布于 2024-12-08 15:30:37 字数 677 浏览 0 评论 0原文

您不能对自己的帖子进行投票 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 技术交流群。

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

发布评论

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

评论(1

〃温暖了心ぐ 2024-12-15 15:30:37

虽然我意识到这可能不再对您有用,但我们使用 此模型来自 OdeToCode,我建议您阅读。

在我们的例子中,我们通常为我们的扩展创建了一个接口,而不仅仅是像 Scott 那样为实体配置创建了一个接口,这使我们不仅可以加载配置,还可以为每个扩展添加工厂和测试数据,向核心应用程序等。

我们的 OnModelCreating 看起来像这样:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // load core object mappings
    modelBuilder.Configurations.Add(new UserConfiguration());
    modelBuilder.Configurations.Add(new PermissionConfiguration());

    // get plugin assemblies
    var catalog = new DirectoryCatalog("bin");
    var container = new CompositionContainer(catalog);
    container.ComposeParts();
    var plugins = container.GetExportedValues<IPlugin>();

    // load plugin object mappings
    foreach (IPlugin plugin in plugins)
    {
        plugin.RegisterDomainEntities(modelBuilder.Configurations);
    }

    base.OnModelCreating(modelBuilder);
}

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:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // load core object mappings
    modelBuilder.Configurations.Add(new UserConfiguration());
    modelBuilder.Configurations.Add(new PermissionConfiguration());

    // get plugin assemblies
    var catalog = new DirectoryCatalog("bin");
    var container = new CompositionContainer(catalog);
    container.ComposeParts();
    var plugins = container.GetExportedValues<IPlugin>();

    // load plugin object mappings
    foreach (IPlugin plugin in plugins)
    {
        plugin.RegisterDomainEntities(modelBuilder.Configurations);
    }

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