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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0