为什么在生成外键时不添加下划线?

发布于 2025-02-04 18:49:20 字数 1367 浏览 3 评论 0原文

我正在从EF 6迁移到EF Core(代码 - 第一个方法),但是当我运行系统时,我会收到一个错误

无效列名称tableenametableId

问题是该列被称为tablename_tableId

出于某种奇怪的原因,EF Core在创建迁移时不会添加下划线,从而导致错误。有什么方法可以解决这种行为?我一直在寻找到处的地方,我找不到发生这种情况的任何其他情况(对于我能找到的,EF核心的正确行为是在生成迁移时添加下划线)

数据库是使用EF6生成的数据库在列名上下调。

这是两个实体的一个示例(系统上约有50个实体)

namespace Testing
{
    public class Model1
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [MaxLength(50)]
        public string Name { get; set; }

        [MaxLength(100)]
        public string Description { get; set; }

        [Column(TypeName = "datetime2")]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy HH:mm}")]
        public DateTime RegistrationDate { get; set; }

        [Required]
        public bool IsActive { get; set; }

        public virtual Model2 Model2 { get; set; }
    }

    public class Model2
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Model2Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        public virtual Model3 Model3 { get; set; }

        public virtual Model4 Model4 { get; set; }
    }
}

I'm migrating from EF 6 to EF Core (code-first approach) but when I run the system I get an error

Invalid column name TableNameTableId

The problem is that the column is called TableName_TableId.

For some strange reason, EF Core is not adding the underscore when creating the migration, causing an error. There's any way to fix that behaviour? I've been looking everywhere and I can not find any other case where this is happening (for what I could find, the correct behaviour of EF Core is to add the underscore when generating a migration)

The database was generated using EF6 which adds the underscore on the column names.

This is an example of two entities (There are approx 50 on the system)

namespace Testing
{
    public class Model1
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

        [MaxLength(50)]
        public string Name { get; set; }

        [MaxLength(100)]
        public string Description { get; set; }

        [Column(TypeName = "datetime2")]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy HH:mm}")]
        public DateTime RegistrationDate { get; set; }

        [Required]
        public bool IsActive { get; set; }

        public virtual Model2 Model2 { get; set; }
    }

    public class Model2
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Model2Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        public virtual Model3 Model3 { get; set; }

        public virtual Model4 Model4 { get; set; }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文