如何指定实体框架核心表映射

如何指定实体框架核心表映射

本文介绍了如何指定实体框架核心表映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的Entity Framework ASP Core Application,它可以工作,但我不知道为什么:

I've made a simple Entity Framework ASP Core Application which 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.

我的问题是:

  1. 如果我不希望模型名称与数据库完全相同,如何指定表显式表映射?

  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?

如何指定自定义列映射.

How do I specify Custom Column Mapping.

我必须为主键/外键指定任何特殊内容

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

修改

要澄清

  1. 说我在数据库MyAccounts中有一个表,我想将该表映射到实体Accounts.

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

说我有一个列密码,我想将其映射到POCO属性PasswordHash

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

推荐答案

  1. 要指定数据库表的名称,可以使用属性或流畅的API:

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

使用属性:

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

使用Fluent API:

Using Fluent API:

public class YourContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Language>(entity => {
            entity.ToTable("MyAccountsTable");
        });
    }
}

  • 要手动命名列,这非常相似,您可以使用属性或流畅的API:

  • To name your columns manually, it's very similar and you can use an attribute or the fluent API:

    使用属性:

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

    使用Fluent API:

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

  • 这篇关于如何指定实体框架核心表映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-15 14:54