我想基于现有数据库生成模型——我认为它就像编写模型、添加 DbContext 类和配置连接字符串一样简单:
namespace MyProject.Models
{
public class Account
{
public int Id { get; set; }
public string Email { get; set; }
public string Name { get; set; }
}
public class AccountDBContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
}
}
使用简单的样板控制器
public ActionResult Index()
{
return View(db.Accounts.ToList());
}
样板视图,我不会在这里发布,它列出了我们返回的 db 对象的所有成员。
最后,一个连接字符串:
<add name="AccountDBContext" providerName="System.Data.SqlClient" connectionString="[server connection string]" />
唯一的问题是我没有看到任何条目正在显示。它肯定是在连接,但不是检索或显示特定数据库包含的数据。我错过了什么吗?
编辑
好的,所以第一件事是:它没有连接。我的连接字符串名称中有错字。它达到了默认值,随后什么也没有显示。
不过,我现在得到的是The model backing the 'AccountDBContext' context has changed since the database was created.
这是因为我的模型与数据库包含的内容不完全匹配吗?