【发布时间】:2016-02-22 05:53:43
【问题描述】:
我是 ASP.net 的菜鸟,不知道为什么没有创建控制器,在教程中我将它逐字复制它工作正常,允许我创建数据库。
按照本教程的结构,但以我自己的方式(只是改了几个名字)-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 没有定义键。定义键 这个实体类型。 Donations:EntityType:EntitySet 'Donations' 是基于 在没有定义键的“慈善”类型上。
【问题讨论】:
-
您的模型没有主键。尝试添加一个 Id 字段。
-
我的日子,谢谢!
-
还知道如何打开文本文件读取和显示内容吗?我需要为此创建一个单独的表单吗?
-
您可以使用 File.ReadAllText() 在控制器中读取文件的所有内容,然后将它们传递给视图。
-
谢谢兄弟,不胜感激:)
标签: c# asp.net asp.net-mvc