开发者

MEF and loading EntityTypeConfiguration in runtime

开发者 https://www.devze.com 2023-04-11 08:59 出处:网络
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 extensionhas its

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' li开发者_StackOverflow中文版brary, 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 ?


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号