无法使用实体框架创建控制器 - 无法检索“ ”的元数据

2024-04-27

我是 ASP.net 的菜鸟,不知道为什么没有创建控制器,在教程中,我将它从一个单词复制到另一个单词,它工作得很好,允许我创建一个数据库。

遵循本教程的结构,但以我自己的方式(只是更改一些名称)-http://www.asp.net/mvc/overview/getting-started/introduction/adding-a-model http://www.asp.net/mvc/overview/getting-started/introduction/adding-a-model

慈善.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; }
    }
}

网络配置:

<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 :

“无法检索 CharityWebsite.Models.Charity 的元数据” CharityWebsite.Models.Charity 没有定义键。定义密钥为 这个实体类型。 Donations:EntityType:EntitySet 'Donations' 是基于 在没有定义键的类型“Charity”上。


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

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

这应该可以解决问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用实体框架创建控制器 - 无法检索“ ”的元数据 的相关文章

随机推荐