none
Entities in different projects RRS feed

  • Question

  • Hi,

    I'm developing a project using MEF for a modular development and I'm trying to figure out whether I can use EF 4.1 Code first approach.

    My problem is that:

    1. I have a project (say Module A) that contains the logic of my application. In this component reside the 99% of my entities.
    2. I have one POCO (call it ClassB) in a different project (Module B) which is conceptually part of the application logic and in fact is used from the objects in Module A. Consider, however, that Module B can be not installed. This is why I use MEF conainer and a polymorphic interface: ClassB implements interface IClassB and ModuleA uses IClassB.
    3. My DbContext-inherited class resides in Module A, but it should reference ClassB, without being aware of it and I cannot reference ModuleB from ModuleA.

    It seems like my application structure and EF do not match, but maybe I am missing something.

    One possibility is using non generic DbSet property in DbContext, but I couldn't figure out how to do it.

    Googling around I have only found plain usage of generic version of DbSet(Of TEntity).

    Any suggestion or idea would be highly appreciated.

    Thanks in advance

    Sunday, December 4, 2011 4:19 PM

Answers

  • Hi Alan,

    thanks for your answer.

    Unfortunately I cannot put all the related entities in one single module, because a couple of them are provided by a class which will be present only under certain conditions.

    Anyway, I probably found a solution. My problem was that it is not possible to declare a DbSet(Of IAnInterface). But it is possible to do it using an MustInherit class, declaring DbSet(Of AbstractClassB) . This will preserve my need of poymorphism, without using interfaces:

    My ClassB will not implement  IClassB interface but will inherit from AbstractClassB, which will be known from ModuleA.

    Cheers

    Alberto

    Monday, December 5, 2011 10:52 AM

All replies

  • Hi A.Paglia,

    Welcome!

    According to your description, to tell truth, I'm not familiar with MEF, but you should put the related entities in one Module, thanks for understanding.

    Non generic DbSet is here: http://msdn.microsoft.com/en-us/library/system.data.entity.dbset(v=vs.103).aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 5, 2011 7:11 AM
    Moderator
  • Hi Alan,

    thanks for your answer.

    Unfortunately I cannot put all the related entities in one single module, because a couple of them are provided by a class which will be present only under certain conditions.

    Anyway, I probably found a solution. My problem was that it is not possible to declare a DbSet(Of IAnInterface). But it is possible to do it using an MustInherit class, declaring DbSet(Of AbstractClassB) . This will preserve my need of poymorphism, without using interfaces:

    My ClassB will not implement  IClassB interface but will inherit from AbstractClassB, which will be known from ModuleA.

    Cheers

    Alberto

    Monday, December 5, 2011 10:52 AM