Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specify a different name for table|column name in EF4

I'm using EF4 with CodeFirst

public class People : DbContext
{
    public DbSet<Human> Humans { get; set; }
    public DbSet<Child> Children { get; set; }
}

At the moment, EF looks in the database for the Human table. How can I specify for it to look for Humans instead?

like image 825
Omu Avatar asked Jan 17 '11 13:01

Omu


People also ask

Is column a data Annotation attribute?

The Column attribute can be applied to one or more properties in an entity class to configure the corresponding column name, data type and order in a database table.


1 Answers

You can change table name on Human class:

[Table("Humans")]
public class Human
{
    ...
}

Other way is to use Fluent API:

modelBuilder.Entity<Human>()
    .ToTable("Humans");

Similary you can use ColumnAttribute or HasColumnName method to change the name of column.

like image 134
Ladislav Mrnka Avatar answered Oct 12 '22 12:10

Ladislav Mrnka