实体框架 [Key] 标签未被识别
Posted
技术标签:
【中文标题】实体框架 [Key] 标签未被识别【英文标题】:Entity Framework [Key] tag not being recognised 【发布时间】:2015-01-03 02:54:18 【问题描述】:尽管我有 [Key] 标记并包括 System.ComponentModel.DataAnnotations 命名空间,但我收到没有为我的每个类库类定义键的警告错误,here是我的上下文:
上下文:
namespace Project.Data
public class ProjectContext : DbContext, IProjectContext
public ProjectContext(string connString)
: base(connString)
this.Configuration.LazyLoadingEnabled = true;
Database.SetInitializer<ProjectContext>(new ProjectInitializer());
this.Database.CreateIfNotExists();
this.Database.Initialize(true);
public IDbSet<Article> Article get; set;
public IDbSet<Brand> Brand get; set;
public IDbSet<Colour> Colour get; set;
public IDbSet<Customer> Customer get; set;
public IDbSet<CustomerCredit> CustomerCredit get; set;
public IDbSet<Delivery> Delivery get; set;
public IDbSet<DesignerTicket> DesignerTicket get; set;
public IDbSet<EuroRate> EuroRate get; set;
public IDbSet<Gift> Gift get; set;
public IDbSet<GZero> GZero get; set;
public IDbSet<InvoiceStock> InvoiceStock get; set;
public IDbSet<PrintOptions> PrintOptions get; set;
public IDbSet<Product> Product get; set;
public IDbSet<ProductLocation> ProductLocation get; set;
public IDbSet<Sale> Sale get; set;
public IDbSet<SaleAccount> SaleAccount get; set;
public IDbSet<SalesToWeb> SalesToWeb get; set;
public IDbSet<Shop> Shop get; set;
public IDbSet<Staff> Staff get; set;
public IDbSet<Ticket> Ticket get; set;
public IDbSet<Transfer> Transfer get; set;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
上下文接口:
namespace Project.Data
public interface IProjectContext
IDbSet<Article> Article get; set;
IDbSet<Brand> Brand get; set;
IDbSet<Colour> Colour get; set;
IDbSet<Customer> Customer get; set;
IDbSet<CustomerCredit> CustomerCredit get; set;
IDbSet<Delivery> Delivery get; set;
IDbSet<DesignerTicket> DesignerTicket get; set;
IDbSet<EuroRate> EuroRate get; set;
IDbSet<Gift> Gift get; set;
IDbSet<GZero> GZero get; set;
IDbSet<InvoiceStock> InvoiceStock get; set;
IDbSet<PrintOptions> PrintOptions get; set;
IDbSet<Product> Product get; set;
IDbSet<ProductLocation> ProductLocation get; set;
IDbSet<Sale> Sale get; set;
IDbSet<SaleAccount> SaleAccount get; set;
IDbSet<SalesToWeb> SalesToWeb get; set;
IDbSet<Shop> Shop get; set;
IDbSet<Staff> Staff get; set;
IDbSet<Ticket> Ticket get; set;
IDbSet<Transfer> Transfer get; set;
[Key] 修饰类示例:
namespace Project.Data
public class Article
[Key]
public int ArticleID;
public bool IsCore;
public string Make;
public string Product;
public decimal Sale;
public string Department;
public string Scale;
public string Detail;
public DateTime InDate;
public decimal Reduce;
public bool IsOnSale;
public string VAT;
public bool IsOnWeb;
public string ProductCode;
public string Pick;
public string MemoDetail;
public string LOC;
public string ColourCode;
public bool StatusFlag;
public string Terminal;
尽管在文章上放置了 [Key],但我收到文章类的以下消息,如下所示,并且每个类都重复此消息:
Project.Data.Article: : EntityType 'Article' has no key defined. Define the key for this EntityType.
有人看到我在这里做错了吗?将不胜感激
【问题讨论】:
【参考方案1】:通过包含 get; set; 在声明的末尾
【讨论】:
感谢愚蠢的错误,我一辈子都看不到它 :) @Jay 别担心,很容易错过!以上是关于实体框架 [Key] 标签未被识别的主要内容,如果未能解决你的问题,请参考以下文章