为什么在生成外键时不添加下划线?
我正在从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论