Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Specify Entity Framework Core Table Mapping?

I've made a simple Entity Framework ASP Core Application that works but I do not know why:

I've made a context like this:

public class AstootContext : DbContext {     public AstootContext(DbContextOptions<AstootContext> options)         : base(options)     { }      public DbSet<Account> Accounts { get; set; }     public DbSet<User> Users { get; set; } } 

And I have two tables with models like this:

public class Account {     public int Id { get; set; }     public string Username { get; set; }     public string PasswordHash { get; set; }     public DateTime Created { get; set; }      List<User> Users { get; set; } }  public class User {     public int Id { get; set; }     public string FirstName { get; set; }     public string LastName { get; set; }     public string Email { get; set; }     public DateTime Birthday { get; set; }     public Account Account { get; set; } } 

The interesting thing is that when I run my application it actually can pick up the data. It just seems weird because I have not specified any table mapping. I'm assuming this just automaps because the specified tables are the same name.

My questions are:

  1. How do I specify Table explicit table mapping in case I do not want my model names to be exactly the same as the DB?

  2. How do I specify Custom Column Mapping.

  3. Is there anything special I have to specify for Primary/Foreign Keys

edit

To clarify

  1. Say I had a table in the DB MyAccounts and I wanted to map that to an entity Accounts.

  2. Say I had a column password and I wanted that to map to a POCO property PasswordHash

like image 610
johnny 5 Avatar asked Feb 12 '17 17:02

johnny 5


People also ask

How do I map a table in Entity Framework?

Map Entity to Table. Code-First will create the database tables with the name of DbSet properties in the context class, Students and Standards in this case. You can override this convention and give a different table name than the DbSet properties, as shown below.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

How do I set primary key in Entity Framework?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.


1 Answers

  1. To specify the name of the database table, you can use an attribute or the fluent API:

    Using Attributes:

     [Table("MyAccountsTable")]  public class Account  {       public string PasswordHash { get; set; }  } 

    Using Fluent API:

     public class YourContext : DbContext  {      protected override void OnModelCreating(ModelBuilder builder)      {          builder.Entity<Language>(entity => {              entity.ToTable("MyAccountsTable");          });      }  } 
  2. To name your columns manually, it's very similar and you can use an attribute or the fluent API:

    Using Attributes:

     public class Account  {      [Column("MyPasswordHashColumn")]      public string PasswordHash { get; set; }   } 

    Using Fluent API:

     public class YourContext : DbContext  {      protected override void OnModelCreating(ModelBuilder builder)      {          builder.Entity<Language>(x => x              .ToTable("MyAccountsTable")              .Property(entity => entity.PasswordHash)                  .HasColumnName("MyPasswordHashColumn")          );      }  } 
like image 103
DavidG Avatar answered Sep 18 '22 04:09

DavidG