none
Multiple EDMX files in different projects sharing entities RRS feed

  • Question

  • Hi,

    This is my first post so apologies for any mistakes.

    My Issue:

    I have a number of web applications that all connect to the same database. Each application uses a common set of class libraries to share code between them. Initially I was going to include one edmx file in one of the DLLs that could be shared accross all applications. However I soon realized that each application would have access to parts of the model that they would never use. I have now created an edmx file per application which have models that only make use of tables relevant to said application. My problem now is that I have a common set of tables that I would like to share between the applications without having to repeat code. E.g. The users table is shared between all apps and I have created a partial class with extended functionality such as a formatted name method. Currently I have to create this partial class in all of my apps and maintain it.

    My Question:

    Is it possible to have a generic edmx for the common tables that will sit in my DLL that I can then attach another edmx file to or extend. Each application will need to be able to navigate from their model to the common model using foreign keys.

    References:

    I have looked at quite a few articles, including (http://social.msdn.microsoft.com/Forums/ar-SA/9f9da524-7cfb-4c0e-9431-b5f0bffd9f7a/referencing-types-in-other-assemblies?forum=adodotnetentityframework) This article suggests that it is possible by manually generating the classes using EdmGen, although this suggests that it can only generate all the tables in a database

    "A conceptual model generated by EdmGen.exe includes all the objects in the database. If you want to generate a conceptual model that includes only specific objects, use the Entity Data Model Wizard."

    Whereas I am looking to only generate classes for tables related to the application (these are separated out into SQL schemas).

    Will this functionality ever be included in upcoming releases of EF or does it break any concepts that have been put in place?

    Thanks for your time


    Tuesday, June 24, 2014 1:44 PM

Answers

  • You don't have a global model. You have a set of stand-alone models that share some entities. EG:

        public class A
        {
            public int ID { get; set; }
        }
    
        public class B
        {
            public int ID { get; set; }
            public int AID { get; set; }
            [ForeignKey("AID")]
            public A A { get; set; }
        }
    
        public class C
        {
            public int ID { get; set; }
            public int AID { get; set; }
            [ForeignKey("AID")]
            public A A { get; set; }
        }
    
    
        public class CoreContext : DbContext
        {
            public DbSet<A> A{ get; set; }
        }
    
        public class BContext : DbContext
        {
            public DbSet<A> A { get; set; }
            public DbSet<B> B { get; set; }
        }
    
    
        public class CContext : DbContext
        {
            public DbSet<A> A { get; set; }
            public DbSet<C> C { get; set; }
        }
    

    David


    David http://blogs.msdn.com/b/dbrowne/


    Wednesday, June 25, 2014 1:59 PM

All replies

  • You can do this, but only in Code-First.  You'd want to use the Code First to and Existing Database workflow.

    In CodeFirst you can share types between DbContext types, so each project can have its own DbContext that has a subset of the types. 

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, June 24, 2014 2:15 PM
  • Hi David,

    Thanks for your quick response. I've looked at the link provided and implemented a test project with Code First EF. However I'm still unsure how my application model can see the entities defined in my global model and traverse the relationships using linq. Could you point me in the right direction.

    thanks

    Wednesday, June 25, 2014 8:44 AM
  • You don't have a global model. You have a set of stand-alone models that share some entities. EG:

        public class A
        {
            public int ID { get; set; }
        }
    
        public class B
        {
            public int ID { get; set; }
            public int AID { get; set; }
            [ForeignKey("AID")]
            public A A { get; set; }
        }
    
        public class C
        {
            public int ID { get; set; }
            public int AID { get; set; }
            [ForeignKey("AID")]
            public A A { get; set; }
        }
    
    
        public class CoreContext : DbContext
        {
            public DbSet<A> A{ get; set; }
        }
    
        public class BContext : DbContext
        {
            public DbSet<A> A { get; set; }
            public DbSet<B> B { get; set; }
        }
    
    
        public class CContext : DbContext
        {
            public DbSet<A> A { get; set; }
            public DbSet<C> C { get; set; }
        }
    

    David


    David http://blogs.msdn.com/b/dbrowne/


    Wednesday, June 25, 2014 1:59 PM