简体   繁体   English

每个表中的列名必须唯一。 表“ UserLogins”中的列名“ department_Id”已多次指定

[英]Column names in each table must be unique. Column name 'department_Id' in table 'UserLogins' is specified more than once

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

Those are 2 table configuration I used in code first approach in C# Entity framework. 这些是我在C#实体框架的代码优先方法中使用的2个表配置。 But After I create the migration code the folowing configuration is created 但是在创建迁移代码之后,将创建以下配置

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

And The following error Message is appeared: Column names in each table must be unique. 并且出现以下错误消息: 每个表中的列名称必须是唯一的。 Column name 'department_Id' in table 'UserLogins' is specified more than once. 表“ UserLogins”中的列名“ department_Id”已多次指定。 Please help me to find the mistake as I am new in code first approach in c# Entity Framework? 请帮助我发现错误,因为我是c#Entity Framework中代码优先方法的新手?

userlogin表创建代码中有两个department_id删除1行Department_id = c.int()

EF adds department_id for UserLogin.department and Department_Id for Department.userLogin collection. EF增加了department_idUserLogin.departmentDepartment_IdDepartment.userLogin集合。 Seems like the framework couldn't understand that the entities are connected by these properties because of extra UserLogin properties in Department . 似乎框架无法理解这些实体是通过这些属性连接的,因为Department有额外的UserLogin属性。 So just help EF to connect them with help of InverseProperty 因此,只需借助InverseProperty帮助EF将它们连接起来InverseProperty

//[InverseProperty("department")] for C# 5 and lower
[InverseProperty(nameof(UserLogin.department))]
public virtual ICollection<UserLogin> userLogin { get; set; }

Use an Id that makes sense to the class and stands on its own. 使用对班级有意义并独立存在的ID。 An ID of ID is not unique. ID的ID不是唯一的。

Use something like this for the IDs: 对ID使用以下内容:

public int LoginID { get; set; }
public int DeptID { get; set; }

You will have less problems with the migrations that way. 这样,您的迁移问题将更少。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个表中的列名必须唯一。 表“ HumanCustomers”中的列名“ LastName”已多次指定 - Column names in each table must be unique. Column name 'LastName' in table 'HumanCustomers' is specified more than once 每个表中的列名必须唯一。 表Entity1&#39;中的列名&#39;Id&#39;被多次指定 - Column names in each table must be unique. Column name 'Id' in table Entity1' is specified more than once 每个表中的列名必须是唯一的例外 - Column names in each table must be unique exception 错误:每个表中的列名必须是唯一的 - Error: Column names in each table must be unique Entity Framework Core 错误“每个表中的列名必须是唯一的” - Entity Framework Core error "Column names in each table must be unique" 实体框架:迁移每个表中的列名必须是唯一的 - Entity Framework: migration Column names in each table must be unique 不打扰的客户端验证规则中的验证类型名称必须唯一。 多次看到以下验证类型:必需 - Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required 多次指定列名EF代码首次迁移 - Column name is specified more than once EF Code First Migration 在 SET 子句中多次指定列名“phoneNumber” - The column name 'phoneNumber' is specified more than once in the SET clause EF核心标识外键引发每个表中的列名必须唯一 - EF Core Identity Foreign Key throws Column names in each table must be unique
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM