1

我是ASP.NET Core的新手。我正在使用带有EF Core的VS2017 ASP.NET Core来构建应用程序,该应用程序允许我管理保存在数据库表中的每日采购订单。如何在运行时使用EF Core创建数据库表?

在Web上有一个输入文本框,允许我输入表名,当我单击创建按钮时,将创建新表。我会每天创建一个新表格,例如“Order25072017”,“Order26072017”等等。

1)如何在asp.net核心MVC中使用EF核心以编程方式创建新表?

这些新表使用相同的“订单”模型/模式,获取订单列表的代码是“_context.Order25072017.ToList();”。我打算用表名列表创建下拉列表。选中后,它将允许我从所选表格中获取订单列表。

2)我需要在OnModelCreating()中做什么? 3)如何在我的查询中更改表名_context。{newtable} .ToList()在运行时?

的DB语境:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
     : base(options) 
    { 
    } 

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

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

的型号:

public class Order 
{ 
    public int ID { get; set; } 
    [Required] 
    public string Product { get; set; } 
    public int Price { get; set; } 
    public int Discount { get; set; } 
} 

构造在OrderController:

private readonly ApplicationDbContext _context; 
public OrderController(ApplicationDbContext context) 
{ 
    _context = context; 
} 
+0

看到这个[Repo](https://github.com/Arch/UnitOfWork)实现** ChangeTable **,你可以从中得到它的想法。 –

+0

我不确定这是否正确。恕我直言,使用包含日期的附加(索引)列和保留单个表格是更好的方法。 – MarkusSchaber

回答

相关问题