I have gotten the question several time over the last few weeks and usages for the fluent configuration of Code First POCO classes where the database does not match the convention. Here is a simple example on how to address this.
Take a product for example.
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string ProductNumber { get; set; }
public bool MakeFlag { get; set; }
}
To configure this to use different column names for the properties you simply need to add a configuration for that class like so.
public class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
this.ToTable("Product","Production");
this.HasKey(x => x.ID);
this.Property(x => x.ID).HasColumnName("ProductID");
}
}
Notice how the inheritance tells it which class it is setting up a type configuration for.
Then you load the configuration to the context in an override like so.
public class AdventureWorkContext : DbContext
{
public AdventureWorkContext(string adventureWorks)
: base(adventureWorks)
{
this.Database.CompatibleWithModel(false);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ProductConfiguration());
base.OnModelCreating(modelBuilder);
}
public DbSet<Product> Products { get; set; }
}
a12254fe-1903-43f3-910a-eed4d44ef5bc|1|5.0
Entity Framework, Data Access