Polymorphism with First code and DBContext

We have a project in which we have several solutions, all of which have a common base library. Our main library has all of our default objects, data context, and utilities that work with this context. I would like the web project to expand the main context, but it seems that I always get either a migration or a multiple database. In fact, I want to have

namespace Core
{
    public class BaseContext : DbContext
    {
        DBSet<SomeCoreEntity> CoreEntities {get;set;}
    }
}

Then we have

namespace Web
{
    public class DerivedContext : Core.BaseContext
    {
        DBSet<SomeNewEntity> NewEntities {get;set;}
    }
}

From here I want the core to have

baseContextInstance.CoreEntities

and

derivedContextInstance.CoreEntites

to refer to the same table, but the derived context would also have access to NewEntites. What is the right way to do this? Is anything special needed in web.config, possibly with DatabaseInitializerForType? I looked around and can not find good documentation on how to do this.

+4
1

, , , , ConnectionString. , , .

NB: update-database Enable-Migrations . , .

( DbSet):

namespace Core
{
    public partial class BaseContext : DbContext
    {
        public BaseContext()
        {

        }

        public BaseContext(string connectionString)
            : base(connectionString)
        {

        }

        public DbSet<SystemUser> SystemUser { get; set; }
    }

    public partial class SystemUser
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public string Forename { get; set; }
        public string Surname { get; set; }
    }
}

( ):

namespace Web
{
    public class DerivedContext : Core.BaseContext
    {
        public DerivedContext()
            : base("BaseContext")
        {

        }

        public DbSet<Order> Order { get; set; }
    }

    public class Order
    {
        public int Id { get; set; }
        public double Quantity { get; set; }
        public string Item { get; set; }
    }
}

, . :

  • BaseContext , ( ) - DbContext
  • "BaseContext" BaseContext

connectionstring .config - BaseContext .

<connectionStrings>
    <add name="BaseContext" connectionString="Data Source=.;Initial Catalog=TestDb;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

-, (, - ):

Enable-Migrations

AutomaticMigrations true

public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

, -, PM:

Update-Database

SystemUser, Order ( TestDb ).


:

, , SystemUsers Orders.

:

using (var ctxt = new Core.BaseContext())
{
    var user1 = new Core.SystemUser()
    {
        Forename = "Obsidian",
        Surname = "Phoenix"
    };

    ctxt.SystemUser.Add(user1);

    ctxt.SaveChanges();
}

using (var ctxt = new DerivedContext())
{
    var user2 = new Core.SystemUser()
    {
        Forename = "John",
        Surname = "Doe"
    };

    ctxt.SystemUser.Add(user2);

    ctxt.SaveChanges();


    var users = ctxt.SystemUser.ToList();

    users.ForEach(u => Debug.WriteLine(string.Format("{0} {1}", u.Forename, u.Surname)));

    var order = new Order()
    {
        Id = 1,
        Item = "Test Item",
        Quantity = 1
    };

    ctxt.Orders.Add(order);

    ctxt.SaveChanges();
}

, BaseContext, DerivedContext / SystemUser, DerivedContext / Order.

+1

All Articles