Home > Blockchain >  ASP.NET MVC - Multiple tables (entities) with the same model without restarting every time
ASP.NET MVC - Multiple tables (entities) with the same model without restarting every time

Time:09-18

I have problems how to properly set DbContext. Let's say I have a .db file with tables "Orders1" and "Orders2". Both based on OrderModel. Is it possible to configure app (probably DbContext) that way, so if I will add third table "Orders3" etc. also based on OrderModel, I will have access to it without changing how app is configured, and re-compile (restart) whole app.

I try to make it using SharedTypeEntity in DbContext, but then ,I guess, I need to add every SharedTypeEntity into builder in i.e. OnModelCreating method. Which means to me, if I will add "Orders3" table to .db, I will also need to add it to OnModelCreating as SharedTypeEntity, and re-compile app to apply those changes.

This is what I have right now:

DbContext:

public class TestDbContext : IdentityDbContext<IdentityUser>
    {
        public TestDbContext(DbContextOptions<TestDbContext> options)
            : base (options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.SharedTypeEntity<OrderModel>("OrdersData");
            builder.SharedTypeEntity<OrderModel>("Orders1Data");
        }

    }

Controller:

public class TestController : Controller
    {

        public TestController(TestDbContext db)
        {
            Db = db;
        }

        public IActionResult Index()
        {
            var orders1Data = Db.Set<OrderModel>("Orders1Data");
            var ordersData = Db.Set<OrderModel>("OrdersData");
            return View();
        }

        private readonly TestDbContext Db;
    }

CodePudding user response:

I was finally able to find out solution for my own problem. Instead of SharedTypeEntity, I create single one, and in controller I am using RawSqlQuery:

DbContext

public class TestDbContext : DbContext
    {

        public TestDbContext(DbContextOptions<TestDbContext> options)
            : base (options)
        {
        }

        public DbSet<OrderModel>? OrdersData { get; set; }

    }

Controller

public class TestController : Controller
    {

        public TestController(TestDbContext db)
        {
            Db = db;
        }

        public IActionResult Index()
        {
            OrderModel[] models1 = new OrderModel[0];
            OrderModel[] models = new OrderModel[0];
            if (Db.OrdersData != null)
            {
                models = Db.OrdersData.FromSqlRaw("SELECT * FROM OrdersData").ToArray();
                models1 = Db.OrdersData.FromSqlRaw("SELECT * FROM Orders1Data").ToArray();
            }
            return View(models1);
        }

        private readonly TestDbContext Db;
    }

Now, I can use i.e. string interpolation to set query table to whatever I want, and I can create more tables with the same type, and have access to it without restarting/rebuilding the whole application

  • Related