代码优先实体配置ToTable问题

时间:2010-08-04 17:52:16

标签: entity-framework code-first ctp

如果我在ToTable覆盖中指定OnModelCreating,请执行以下操作:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<Team>()
    .MapSingleType()
    .ToTable("my_teams");    
}

它会创建dbo.my_teams,但是如果我把它分成这样的类:

public class TeamMap : EntityConfiguration<Team>
{
  public TeamMap()
  {
     MapSingleType()
     .ToTable("my_teams");
  }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Configurations.Add(new TeamMap());
}

它会创建dbo.Teams。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这似乎是MapSingleType()重载中的一个错误,它没有参数...感谢找到它!作为解决方法,如果使用MapSingleType()的另一个重载指定所有列映射,您将获得预期的结果。例如:

public class TeamContext : DbContext
{
    public DbSet<Team> Teams { get; set; }

    public class TeamMap : EntityConfiguration<Team>
    {
        public TeamMap()
        {
            MapSingleType(t => new { t.Id, t.Name })
            .ToTable("my_teams");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new TeamMap());
    } 
}

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
}

杰夫