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.

5
  • 1
    Your model does not have a primary key. Try adding an Id field. Commented Feb 21, 2016 at 21:37
  • My days, It worked thanks! Commented Feb 21, 2016 at 21:43
  • Also any idea how I can open text file read and display contents? would I have to create a seperate form for that? Commented Feb 21, 2016 at 21:46
  • You could use File.ReadAllText() to read all the contents of the file, in the controller and then passed them to the view. Commented Feb 21, 2016 at 21:49
  • 1
    Thanks bro, appreciate it :) Commented Feb 21, 2016 at 21:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Btw - C# does not have a bigint type - maybe you mean long or BigInteger
@Teodor Thanks for correcting, i have edited the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.