繁体   English   中英

实体框架动态表名称部分

[英]Entity framework dynamic table name part

我们的表名称包括年份。 看起来像“ Item_2015”“ Item_2014”是否可以与ef配合使用? 我需要生成Item类,但它应与db的年份后缀一起使用。 谢谢

您可以在上下文中添加一个接收后缀作为参数的构造函数,并使用Fluent Api映射表的名称

public class Item
{
  //...
}

public class YourContext: DbContext 
{
    private string suffix="_2015";

    public SchoolDBContext(string _suffix): base() 
    {
       this.suffix=_suffix;
    }

    public DbSet<Item> Items{ get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         // Map an Entity Type to a Specific Table in the Database
         modelBuilder.Entity<Item>().ToTable("Item"+suffix);

        base.OnModelCreating(modelBuilder);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM