0

I am a noob to ASP.net and have no idea why the controller isnt being created, in the tutorial by me copying it from word to word it worked fine allowing me to create a database.

Following the structure of this tutorial but in my own way (just changing a few names)-http://www.asp.net/mvc/overview/getting-started/introduction/adding-a-model

Charity.cs :

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace CharityWebsite.Models
{
    public class Charity

    {
        public String DisplayName { get; set; }
        public DateTime Date { get; set; }
        public Double Amount { get; set; }
        public Double TaxBonus { get; set; }
        public String Comment { get; set; }
    }

    public class CharityDBContext : DbContext
    {
        public DbSet<Charity> Donations { get; set; }
    }
}

Web.Config:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-CharityWebsite-20160221090932.mdf;Initial Catalog=aspnet-CharityWebsite-20160221090932;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="CharityDBContext"
        connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Donations.mdf;Integrated Security=True"
        providerName="System.Data.SqlClient"
    />
</connectionStrings>

Error :

'Unable to retrieve meta data for CharityWebsite.Models.Charity' CharityWebsite.Models.Charity has no key defined. Define the key for this entityType. Donations:EntityType:EntitySet 'Donations' is based on type 'Charity' that has no keys defined.

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Edafy
  • 31
  • 2
  • 10

1 Answers1

1

To Solve the issue try adding a primary key into the model.

[Key]
[Required]        
public long ID{ get; set; }

This should solve the issue.

Srinivas Ramakrishna
  • 1,294
  • 13
  • 21