实体框架[Key]标签未被识别

时间:2014-11-06 14:43:41

标签: entity-framework key entity-framework-6 dbcontext

虽然我有[Key]标签并包含 System.ComponentModel.DataAnnotations 命名空间,但我收到的警告错误是没有为我的每个类库定义任何键是我的背景:

上下文

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 个答案:

答案 0 :(得分:3)

通过包含{get;将类的成员定义为公共属性,而不是像此处那样的公共变量。在声明的末尾设置;}