无法使用实体框架创建控制器-无法检索“"的元数据 [英] Cannot create controller with Entity framework - Unable to retrieve metadata for ' '

查看:84
本文介绍了无法使用实体框架创建控制器-无法检索“"的元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.net的菜鸟,也不知道为什么不创建控制器,在本教程中,我逐字逐句地复制它,可以正常工作,可以创建数据库.

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>

错误:

'无法检索CharityWebsite.Models.Charity的元数据' CharityWebsite.Models.Charity没有定义键.定义密钥 此entityType. Donations:EntityType:EntitySet'捐赠'是基于 没有定义键的慈善"类型.

'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.

推荐答案

要解决此问题,请尝试在模型中添加主键.

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

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

这应该可以解决问题.

这篇关于无法使用实体框架创建控制器-无法检索“"的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆