Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 实体框架代码中的唯一键错误优先_Entity Framework - Fatal编程技术网

Entity framework 实体框架代码中的唯一键错误优先

Entity framework 实体框架代码中的唯一键错误优先,entity-framework,Entity Framework,跟踪错误 “/”应用程序中出现服务器错误 表“dbo.Dept_Master”中的列“Dept_Name”的类型无效,无法用作索引中的键列 描述:测试期间发生未处理的异常 当前web请求的执行。请查看堆栈跟踪 有关错误以及错误来源的详细信息,请参阅 代码 异常详细信息:System.Data.SqlClient.SqlException:列 表“dbo.Dept\u Master”中的“Dept\u Name”的类型无效 用作索引中的键列 当您使用VARCHAR(Max)时,我们将得到此错误。请

跟踪错误

“/”应用程序中出现服务器错误

表“dbo.Dept_Master”中的列“Dept_Name”的类型无效,无法用作索引中的键列

描述:测试期间发生未处理的异常

当前web请求的执行。请查看堆栈跟踪 有关错误以及错误来源的详细信息,请参阅 代码

异常详细信息:System.Data.SqlClient.SqlException:列 表“dbo.Dept\u Master”中的“Dept\u Name”的类型无效 用作索引中的键列

当您使用VARCHAR(Max)时,我们将得到此错误。请尝试使用:

public  class Dept_Master
 {
   [Key]
   public int Dept_Id { get; set; }
   [Index(IsUnique =true)]
   public string Dept_Name { get; set; }
   public bool status { get; set; }           
}
其中n在1和450之间。

当您使用VARCHAR(Max)时,我们将得到此错误。请尝试使用:

public  class Dept_Master
 {
   [Key]
   public int Dept_Id { get; set; }
   [Index(IsUnique =true)]
   public string Dept_Name { get; set; }
   public bool status { get; set; }           
}
其中n在1和450之间

您可以通过两种方式分配唯一密钥

@字符串长度varchar数据类型不需要

您可以通过两种方式分配唯一密钥

@字符串长度varchar数据类型不需要


我猜
Dept\u Name
(n)varchar(max)
,这使得它不符合唯一约束我猜
Dept\u Name
(n)varchar(max)
,这使得它不符合唯一约束
[Column(TypeName = "VARCHAR")]
[StringLength(n)]
[Index(IsUnique = true)]
public string Dept_Name { get; set; }
1)
    [Column(TypeName = "NVARCHAR")]
    [StringLength(2000)]
    [Index(IsUnique = true)]
    public string Dept_Name { get; set; }

2)
   [Column(TypeName = "VARCHAR")]     
   [Index(IsUnique = true)]
   public string Dept_Name { get; set; }