0

I'm currently using EF5 in a project with a legacy database. The legacy application uses dynamically build tables (xxxx_year, yyyy_year) to store "year based data". I've been trying to find a way to dynamically map the ef entities (xxxx, yyyy, etc) to the tables, based on the year property value, but I always end up getting the "The model backing the context has changed since the database was created." error. Can anyone give me some ideas on how to accomplish this ?

I found some old blog posts talking about edm mapping, where we can separate mapping tables based on some property value (kind of horizontal partitioning), but I can't find any pointers on how to accomplish the same using code first.

Thanks, P

4

1 回答 1

0

在每个域对象的映射配置中,您可以告诉 EF 实体的对应表名称与实体名称本身不同。

如果您的类名为 YyyyYear,它可以通过在其映射文件中指定名称来指向名为“2012_year”的表。

例如

// 每个 db 表 1 个实体类
公共课 YyyyYear
{
  公共 int ID { 获取;放; }
}

// 1个实体映射文件
使用 System.Data.Entity.ModelConfiguration;
公共类 YyyyYearMap:EntityTypeConfiguration
{
  公共 YyyyYearMap()
  {
    this.HasKey(t => t.Id);
    this.ToTable("2012_year");
  }
}

// 你的数据库上下文类(从 DbContext 派生)
使用 System.Data.Entity;
公共类 MyDbContext:DbContext
{
  // 为每个实体/表设置 1 个数据库
  公共数据库集 YyyyYears { 获取;放; }

  受保护的覆盖无效 OnModelCreating(DbModelBuilder modelBuilder)
  {
    // 每个实体/表有 1 个映射文件
    modelBuilder.Configurations.Add(new YyyyYearMap());
  }
}

我不确定这是否是您要查找的内容,但我有一篇博客文章,其中包含分步说明、工作示例以及如何解决常见问题。

http://wakeupandcode.com/entity-framework-code-first-migrations/

希望这可以帮助!

于 2013-10-29T20:50:16.610 回答