Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework core EF Core自动将表名从设置更改为设置_Entity Framework Core - Fatal编程技术网

Entity framework core EF Core自动将表名从设置更改为设置

Entity framework core EF Core自动将表名从设置更改为设置,entity-framework-core,Entity Framework Core,我对EF Core生成的表名有问题。有人能帮忙吗 我有一个设置实体,EF配置为 public class SettingConfiguration : IEntityTypeConfiguration<Setting> { public void Configure(EntityTypeBuilder<Setting> builder) { builder.HasKey(c => c.Id); builder.ToTa

我对EF Core生成的表名有问题。有人能帮忙吗

我有一个
设置
实体,EF配置为

public class SettingConfiguration : IEntityTypeConfiguration<Setting>
{
    public void Configure(EntityTypeBuilder<Setting> builder)
    {
        builder.HasKey(c => c.Id);
        builder.ToTable<Setting>(typeof(Setting).Name.ToLower());
        builder.Property(c => c.Id).ValueGeneratedOnAdd().IsRequired().HasColumnName("id");
        builder.Property(c => c.UserId).IsRequired().HasColumnName("userId");
        builder.Property(c => c.ShowEmail).IsRequired().HasColumnName("showEmail");
    }
}
我有另一个实体名为
Post
,EF配置为

public class PostConfiguration : IEntityTypeConfiguration<Post>
{
    public void Configure(EntityTypeBuilder<Post> builder)
    {
        builder.HasKey(c => c.Id);
        builder.ToTable<Post>(typeof(Post).Name.ToLower());
        builder.Property(c => c.Id).ValueGeneratedOnAdd().IsRequired().HasColumnName("id");
        builder.Property(c => c.Title).IsUnicode().IsRequired().HasMaxLength(200).HasColumnName("title");
        builder.Property(c => c.Comment).IsUnicode().IsRequired().HasMaxLength(1000).HasColumnName("comment");
    }
}

这是惯例,您将模型定义为单数,EF将创建复数形式的表名


如果您想摆脱这种情况,请提供一个
[Table(“YourTableName”)]
属性。

顺便说一句,typeof可以替换为nameof。谢谢j4rey,让我试试。但是,不确定为什么不更改
Post
实体。请看我在问题中给出的例子。实际上,我有很多其他实体,但只有
设置
有一些问题。Hi@j4rey,似乎添加属性可以解决问题,但我不明白为什么问题只发生在
设置
而其他实体
public class PostConfiguration : IEntityTypeConfiguration<Post>
{
    public void Configure(EntityTypeBuilder<Post> builder)
    {
        builder.HasKey(c => c.Id);
        builder.ToTable<Post>(typeof(Post).Name.ToLower());
        builder.Property(c => c.Id).ValueGeneratedOnAdd().IsRequired().HasColumnName("id");
        builder.Property(c => c.Title).IsUnicode().IsRequired().HasMaxLength(200).HasColumnName("title");
        builder.Property(c => c.Comment).IsUnicode().IsRequired().HasMaxLength(1000).HasColumnName("comment");
    }
}
migrationBuilder.CreateTable(
                name: "post",
                columns: table => new
                {
                    id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    title = table.Column<string>(maxLength: 200, nullable: false),
                    comment = table.Column<string>(maxLength: 1000, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_post", x => x.id);
                });