2

I have a question about inheriting a custom DbContext class.

I have created a custom DbContext class called BaseContext in namespace 1 using C# Ado.net entity model generator as shown here:

namespace NS1
{
   public partial class BaseContext: DbContext
   {
        public BaseContext(): base("name=BaseContext")            
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<PLAYERS> PLAYERS { get; set; }   
   }
}

And in the other namespace, I have created another context which uses the same database but its some other tables.

namespace NS2
{
   public partial class ExtensionContext: DbContext
   { 
       public ExtensionContext(): base("name=ExtensionContext")
       {
       }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       { 
         throw new UnintentionalCodeFirstException();
       }

       public virtual DbSet<CRICKET_PLAYERS> CRICKET_PLAYERS { get; set; }      
   }
}

Now I am in a situation to use PLAYERS DbSet in the ExtensionContext. I have tried to inherit the BaseContext from ExtensionContext and pass the ExtensionContext connection string to the BaseContext constructor which in turn sends to the DbContext.

But during implementation I get an exception stating

The PLAYERS name does not exist in the current ExtensionContext

Please give me some insight into the possibility of reusing the existing DbSet types in the extended context.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Felix
  • 166
  • 1
  • 2
  • 14
  • Runtime exception or the compile-time error? – Wiktor Zychla Nov 02 '14 at 10:49
  • I have solved the problem by simply omitting the creation of .edmx using ADO.Net Entity Model. Instead, I am writing my own POCOs and following the typical code first approach as desc. here http://www.codeproject.com/Tips/661053/Entity-Framework-Code-First-Map. – Felix Nov 03 '14 at 12:42

0 Answers0