我已从我的数据库生成模型。其中一个模型如下:
namespace LV.Models
{
using System;
using System.Collections.Generic;
public partial class Ac
{
public int IdAc { get; set; }
public int IdEmp { get; set; }
public Nullable<System.DateTime> dtd{ get; set; }
public Nullable<int> Sn{ get; set; }
public Nullable<bool> A{ get; set; }
public Nullable<int> IdSi{ get; set; }
public Nullable<bool> Gest{ get; set; }
public Nullable<int> IdResp { get; set; }
public string SigXML { get; set; }
public Nullable<bool> Read { get; set; }
public Nullable<System.DateTime> EndDate{ get; set; }
public string dpd{ get; set; }
public string gda { get; set; }
public string typeaq { get; set; }
public byte[] attch { get; set; }
public string exten { get; set; }
public virtual AC emps { get; set; }
}
}
当我尝试创建一个Controller时,它会显示has no key defined
。我尝试了其他帖子的解决方案,其中说我必须使用[Key]
并创建我的控制器,但是当我运行项目时,它会抛出另一个例外the following table cannot be created
。它说,因为该表已经存在。
我使用Visual Studio 2013 Express和EF 6 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
主键是idAc
。它也是身份。如果我查看图表,那就是它所说的。
从我所看到的,映射是好的。我的所有密钥,关系,主键,标识字段都在图中标识。
答案 0 :(得分:0)
试试这个:
[Key]
[Column("idAc")]
public int IdAc { get; set; }
我怀疑映射可能区分大小写,因此,当您指定[Key]
时,EF只会查找名为IdAc
的列,而不是找到它。
我知道您正在使用EDM设计器,因此如果不能解决问题,那么EDM设计人员可能正在弄乱映射。尝试将该属性重命名为与该列相同(即idAc
)或检查this workaround。
另请注意,如果您不使用[Key]
或Fluent API .HasKey
方法来指示哪个属性是关键,则默认情况下EF uses the code-convention使用属性与实体同名(最后有或没有“Id”)。
答案 1 :(得分:0)
我找到了解决方案。当我生成edmx文件时,他问我是否要保存连接字符串。那时我选择不保存它。后来我发现在我的edmx属性中有一个空的连接字符串。
我删除了edmx并生成了一个新的,这次保存了连接字符串。从那时起,一切顺利。