我已经从数据库中生成了模型。其中一种模型看起来像:
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; }
}
}
当我尝试创建控制器时,它说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
。这也是身份。如果我检查图表,那就是所说的。
从我所看到的,映射很好。我所有的密钥,关系,主要键,身份字段,在图中标识。
尝试以下:
[Key]
[Column("idAc")]
public int IdAc { get; set; }
我怀疑映射可能对情况敏感,因此,当您指定[Key]
时,EF只是寻找一个名为IdAc
的列,而找不到它。
我知道您正在使用EDM设计器,因此,如果无法解决它,则可以使EDM设计师与映射混乱。尝试将属性重命名为与列(即idAc
)相同或检查此解决方法。
另外,请注意,如果您不使用[Key]
或流利的API .HasKey
方法来指示哪个属性是关键,则默认情况下,EF使用代码进行使用,使用具有与实体相同名称的属性(使用)或没有" ID")。
我找到了解决方案。当我生成EDMX文件时,他问我是否要保存连接字符串。那时,我选择不保存它。后来我看到了我的EDMX属性中有一个空连接字符串。
我删除了EDMX,并生成了一个新的,这次保存了连接字符串。由于这一刻,一切都可以顺利进行。