Jay
Jay

Reputation: 3082

Entity Framework [Key] tag not being recognised

I am getting warning errors of no key having been defined for each of my class library classes despite the fact that I have the [Key] tag and including the System.ComponentModel.DataAnnotations namespace, here is my context:

Context:

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>();
    }

}
}

Context Interface:

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] decorated class example:

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;

}
}

Despite have the [Key] placed on Article I am getting the following message for the article class as shown below and this is repeated for each of the classes:

Project.Data.Article: : EntityType 'Article' has no key defined. Define the key for this EntityType.

Anyone see what I am doing wrong here? would be greatly appreciated

Upvotes: 1

Views: 71

Answers (1)

Harko
Harko

Reputation: 413

Define the members of your class as public properties as opposed to public variables like you have here, by including {get; set;} at the end of the declaration

Upvotes: 3

Related Questions