c# - 每个表中的列名必须是唯一的。多次指定表“UserLogins”中的列名“department_Id”
问题描述
class UserLogin
{
[Key]
public int id { get; set; }
[StringLength(50)]
public string userName { get; set; }
[StringLength(20)]
public string password { get; set; }
public virtual Modules module { get; set; }
public virtual Company company { get; set; }
public virtual Department department { get; set; }
}
class Department
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(50)]
public string DeptName { get; set; }
[Required]
[StringLength(50)]
public string DeptGroup { get; set; }
[Required]
[StringLength(15)]
public string userComputer { get; set; }
[Required]
public DateTime enterDate { get; set; }
[Required]
public virtual UserLogin createdBy { get; set; }
public DateTime updateDate { get; set; }
public virtual UserLogin updatedBy { get; set; }
public virtual ICollection<UserLogin> userLogin { get; set; }
这些是我在 C# Entity 框架的代码优先方法中使用的 2 个表配置。但是在我创建迁移代码之后,将创建以下配置
CreateTable(
"dbo.UserLogins",
c => new
{
id = c.Int(nullable: false, identity: true),
userName = c.String(maxLength: 50),
password = c.String(maxLength: 20),
company_id = c.Int(),
Department_Id = c.Int(),
department_Id = c.Int(),
module_id = c.Int(),
})
.PrimaryKey(t => t.id)
.ForeignKey("dbo.Companies", t => t.company_id)
.ForeignKey("dbo.Departments", t => t.Department_Id)
.ForeignKey("dbo.Departments", t => t.department_Id)
.ForeignKey("dbo.Modules", t => t.module_id)
.Index(t => t.company_id)
.Index(t => t.Department_Id)
.Index(t => t.department_Id)
.Index(t => t.module_id);
CreateTable(
"dbo.Departments",
c => new
{
Id = c.Int(nullable: false, identity: true),
DeptName = c.String(nullable: false, maxLength: 50),
DeptGroup = c.String(nullable: false, maxLength: 50),
userComputer = c.String(nullable: false, maxLength: 15),
enterDate = c.DateTime(nullable: false),
updateDate = c.DateTime(nullable: false),
createdBy_id = c.Int(nullable: false),
updatedBy_id = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.UserLogins", t => t.createdBy_id, cascadeDelete: true)
.ForeignKey("dbo.UserLogins", t => t.updatedBy_id, cascadeDelete: true)
.Index(t => t.createdBy_id)
.Index(t => t.updatedBy_id);
并出现以下错误消息:每个表中的列名必须是唯一的。表“UserLogins”中的列名“department_Id”被指定了多次。 请帮我找出错误,因为我是 c# Entity Framework 中代码优先方法的新手?
解决方案
EF 添加department_id
forUserLogin.department
和Department_Id
forDepartment.userLogin
收集。似乎框架无法理解实体是由这些属性连接的,UserLogin
因为Department
. 因此,只需帮助 EF 在InverseProperty
//[InverseProperty("department")] for C# 5 and lower
[InverseProperty(nameof(UserLogin.department))]
public virtual ICollection<UserLogin> userLogin { get; set; }
推荐阅读
- git - 如何仅在变更集中暂存行删除?
- java - 如何使用 Powermock、easymock、mockito 对 DriverManager.getConnection("") 或 DriverManager.getConnection("","","") 进行单元测试
- r - 行上的R数据帧cumsum
- android - Android,Room 在使用 Robolectric 进行单元测试时失败
- python - UnicodeDecodeError 异常是如何工作的?
- python - miniconda 找不到包
- sql - Oracle REGEXP_COUNT 不转义方括号]
- javascript - 解决快速路线中的承诺
- c# - 如何将下拉列表的多个值从视图发送到控制器
- javascript - 相同表单的多个版本的 Vanilla JavaScript 表单验证