none
Splitting DB Tables into two libraries RRS feed

  • Question

  • Hi,

    Anyone suggest me how to handle the following scenario.

    I have some Db tables that are using in all of my projects so I am creating all these tables in every database (common tables + project tables). Now I have a common data and business libraries that depend on the common tables, I need to split these table entities into two different libs with a single DbContex.

    I am using the dependency injection to pass the db context.

    I am using the following.

    EF 4.1

    EF Repository

    VS 2010.

     

    Regards,

    Hareen.

     


    Hareen
    Saturday, July 2, 2011 6:59 AM

Answers

  • Hello,

    If you embed two entities which have some relationships into different assemblies, I think it should be occurred some error. I suggest you can try to embed some entities which have some relationships into one assembly and embed others that haven't any relationship into one assembly. Then, you can directly using this DLL in other project.

    Best Regards,


    Larcolais Gong[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.

    Tuesday, July 5, 2011 7:17 AM

All replies

  • Hi,

    Yes, you can have this in two different libs with a DbContext for each, but be aware, that if you have one DbContext for the common tables and one for the project tables you can't join tables from model1 with tables from model2 automatically, you'll have to do this manually in your code.

    If you are using DbContext with Code-First, you have to supress the database verification and generation to avoid them to take out eachother. In this post http://blogs.msdn.com/b/adonet/archive/2010/09/02/ef-feature-ctp4-dbcontext-and-databases.aspx at the bottom there is a description called Working With Existing Databases that describes how to disable this. If you are using DbContext with Model-First, the database verification and generation isn't a issue.

     


    --Rune
    Saturday, July 2, 2011 7:13 AM
  • Hi Rune,

     

    Thanks for quick reply, I am using the DB Context with Generic repository and I don’t want to use two contexts just I need to separate the models into two separate libraries.

    Any time the core models don’t require any knowledge about the custom schema.

    Regards,

    Hareen.

     


    Hareen
    Saturday, July 2, 2011 12:10 PM
  • I am not sure really catch you need,  I think your puzzle also puzzle me before.
    the core need is  the model defined in one dll, should be shared by other dll.

    when using EDMX, can not share table models in diferent dll.
    (precisly, common tables, should be defined in each dlls)

    and when Code First, the things changed, 
    my POCO working workround,Solution like this:
    (this still not the best way, I anticipate the  that DbSet can be Created when needed.  not this style: DbSet should hard Coding as Contex's property)


        in common.dll,

        public interface ICommonCtx
        {
            DbSet<TpoTask> TaskSet { get; set; }
            DbSet<TpoDict> DictSet { get; set; }
            .....
        }
        in which we define TpoTask, and TpoDict, will share by others.

     

       in Cy.Dll 
       public TCyCtx, ICommonCtx
        {
             public DbSet<TpoTask> TaskSet { get; set; }
             public DbSet<TpoDict> DictSet { get; set; }
             ...


             public DbSet<TSale> SaleSet { get; set; }
             public DbSet<TBill> BillSet { get; set; }
      
          }

     

           in Gas.Dll
    public TGasCtx, ICommonCtx
    {
        public DbSet<TpoTask> TaskSet { get; set; }
        public DbSet<TpoDict> DictSet { get; set; }
    ...


        public DbSet<TSation> SationSet { get; set; }
        public DbSet<TCard> CardSet { get; set; }

    }


    Saturday, July 2, 2011 2:48 PM
  • Hi,

    It's still note quite clear for me what you want. There is no problem having your classes that defines your entities in different assemblies and the context in a seperate one. You just have to reference your entity assemblies in your assembly that contain the dbcontext and define your datasets there.

    Eg.

    Assembly1

    Employee

    Assembly2

    Customer

    Assembly3

    MyContext with:
    DbSet<Employee>
    DbSet<Customer>

    Is this what you want?


    --Rune
    Saturday, July 2, 2011 5:45 PM
  • Hi,

    My need is exactly Rune's example. but by going this way i will miss the navigational properties in Core model DLL right?

     

    Regards,

    Hareen.

     

     


    Hareen
    Sunday, July 3, 2011 4:32 AM
  • Hello,

    If you embed two entities which have some relationships into different assemblies, I think it should be occurred some error. I suggest you can try to embed some entities which have some relationships into one assembly and embed others that haven't any relationship into one assembly. Then, you can directly using this DLL in other project.

    Best Regards,


    Larcolais Gong[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.

    Tuesday, July 5, 2011 7:17 AM