views:

163

answers:

1

If I specify ToTable in the OnModelCreating override like so:

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

it creates dbo.my_teams, however if I separate it out into classes like so:

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

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

it creates dbo.Teams. Any ideas why?

+1  A: 

This seems like a bug in the MapSingleType() overload that takes no parameters...thanks for finding it! As a work-around, if you specify all of your column mappings using the other overload of MapSingleType(), you'll get the expected result. For example:

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; }
}

Jeff

Jeff
That worked! But now I cannot seem to map a one-to-many relationship with Athletes thrown in the above mix. Using Team.Athletes and Athlete.Team properties.
mxmissile