none
Extending DbContext for modular programming RRS feed

  • Question

  • Hi.

    I am developing a project which consist of a core library and other libraries (as modules). In core biliary I defined main entities like UserAccount, Logging and etc and warp them in a class which is inherited from  DbContext. Every single module has its own entity and model, and every module can be added to main project later, I mean they are not already defined. Consider a scenario where I have create a New module, which contains News, Comments and Category entities, I want to plug this to main application, and also use the DbContext defined in core library, but as you know, in Code-First Approach, it will throw an exception, and want me to re-create the database, because the model has changed. Is there any solution for this problem ? and way to make DbContext extensible ?

    Sunday, September 11, 2011 8:00 AM

Answers

  • Hi persian developer :)

    try deleting the EdmMetadata table in the DbContext's newly generated database, this will remove schema comparing ability and allow you extend your solution. After you do this you just create an new class and appropriate table, add some mapping rules using the fluent api and you're done.

     

    Regards


    Clarity VS Precision
    Sunday, September 11, 2011 9:16 PM

All replies

  • Hi persian developer :)

    try deleting the EdmMetadata table in the DbContext's newly generated database, this will remove schema comparing ability and allow you extend your solution. After you do this you just create an new class and appropriate table, add some mapping rules using the fluent api and you're done.

     

    Regards


    Clarity VS Precision
    Sunday, September 11, 2011 9:16 PM
  • I tried updating EdmMetadata ModelHash before, but did not know any thing about deleting this table, cause I afraid it would make problems. However I tried your solution and it works for now.:)


    And some thing else. what if I wanted to add new entity mapping rules to OnModelCreating in run-time ? I mean consider In core application I have my OnModelCreating as follow :

    public class DemoContext : DbContext

    {

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<Account>();
                modelBuilder.Entity<Logging>();
            }

    }

     

    But when I want to add a new module and use DemoContext for its DAL, I want to add its mapping rules to OnModelCreating, how could it be possible ?


    Monday, September 12, 2011 6:38 AM
  • Sure thing,

    it only serves for comparing schema and detecting changes. (But before a human interaction to the database it's always a good idea to make a backup :))


    Clarity VS Precision
    Monday, September 12, 2011 6:56 AM
  • Yes, mapping rules should be applied on on OnModelCreating event, take a look at this link:

    http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

    and these rules will be applied to your scenario as well.

     

    Thanks and regards


    Clarity VS Precision
    Monday, September 12, 2011 7:08 AM
  • Thnaks Giorgi Zautashvili, and something else.

    Is it possible to add enities in modelBuilder by using Managed Extensibility Framework  (MEF)?

     

    Monday, September 12, 2011 8:22 AM
  • I'm sorry for not being able to answer on this question, maybe you should start a new thread for this particular one. The answer would be interesting for me too.

    Regards


    Clarity VS Precision
    Monday, September 12, 2011 10:31 AM
  • oh, you should not be sorry, you helped me a lot. I hope it would me my last question! after deleting EdmMetadata, I got that I should create tables manually, am I right ?

     

    Monday, September 12, 2011 3:53 PM
  • Yes, you are.

    If you have more question feel free to post, I hope to answer them all :)

     

    Regards


    Clarity VS Precision
    Tuesday, September 13, 2011 5:10 AM
  • Persian developer have you solved the problem with modularity ? Can you share your solution ?
    Sunday, October 16, 2011 4:24 PM
  • Hi temom.

     I asked about my problem here in msdn, but no body answered me. But after spending hours and searching, I found this useful article. I asked the same questions about implementing modularity  in my application and he suggested a way that although was not what I really wanted, but helped me to find a solution. 

    Actually to load every entity`s configueation file to enable modularity in EF, I defined a Interface. This interface actually works as a contract which is used by [Import] and [Export] codes in MEF to load 'modules'. Here my module is an entity configuration class. Then I imported them in my own DbContex derived class, and them to configuration as below :

     

     

     [ImportMany(typeof(IEntityConfiguration), AllowRecomposition = true)]
    
            //public IEnumerable<EntityTypeConfiguration<object>> _Configs { get; set; }
    
            public ObservableCollection<Lazy<IEntityConfiguration, IEntityConfigMetadata>> _Configs { get; set; }
    
    
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                //this.Database.Initialize(false);
    
                //modelBuilder.Configurations.
                System.Data.Entity.Database.SetInitializer<SabteNamDbContext>(null);
    
    
    
                foreach (var item in _Configs)
    
                {
    
                    Type type = item.Value.GetType();
    
    
    
                    dynamic configurationInstance = Activator.CreateInstance(type);
    
                    //this.Configurations.Add(configurationInstance);
    
    
    
                    modelBuilder.Configurations.Add(configurationInstance);
    
                }
    
            }
    
    
    

    Hope it would help you. 

    Tuesday, October 18, 2011 7:04 PM