Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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)
                    .HasField("MyPasswordHashColumn")
            );
        }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...