C# 添加控制器时无法获取元数据mvc4
我正在尝试创建我的第一个mvc类,但在开始时遇到了一个问题。 我在我的托管帐户上使用一个共享数据库,并创建了以下类C# 添加控制器时无法获取元数据mvc4,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在尝试创建我的第一个mvc类,但在开始时遇到了一个问题。 我在我的托管帐户上使用一个共享数据库,并创建了以下类 namespace MvcApplication2.Models { public class tUsers { public int UserID { get; set; } public string Username { get; set; } public string UserEma
namespace MvcApplication2.Models
{
public class tUsers
{
public int UserID { get; set; }
public string Username { get; set; }
public string UserEmail { get; set; }
public string UserPassword { get; set; }
public int GroupId { get; set; }
}
public class tUsersDBContext : DbContext
{
public DbSet<tUsers> tUsers { get; set; }
}
}
namespace mvcapapplication2.Models
{
公营屠宰场
{
public int UserID{get;set;}
公共字符串用户名{get;set;}
公共字符串UserEmail{get;set;}
公共字符串UserPassword{get;set;}
public int GroupId{get;set;}
}
公共类tUsersDBContext:DbContext
{
公共数据库集tUsers{get;set;}
}
}
这是我的连接字符串
<add name="DefaultConnection" connectionString="Data Source=gdfgdfgdfg;Initial Catalog=fsdf_fdsf;Persist Security Info=True;User ID=ddd_reddntal;Password=fgfggfdg" providerName="System.Data.SqlClient" />
我得到的错误是
无法检索“MVCAPApplication2.models.tusers”的一个或多个元数据
在模型生成过程中检测到更多验证错误:
实体类型用户没有受保护的密钥
我的模型名是否需要与表名精确匹配?实体框架无法识别
tUsers
类的主键属性。用Key
属性注释UserID
属性。如果表名与类名不同,请使用table
属性指定表名
尝试对类名使用正确的命名约定。在这种情况下,它应该是User
而不是tUsers
[Table("MyTableName")]
public class tUsers
{
[Key]
public int UserID { get; set; }
public string Username { get; set; }
public string UserEmail { get; set; }
public string UserPassword { get; set; }
public int GroupId { get; set; }
}
请确保已将Id定义为表中的主键,并确保已为模型类正确定义属性。此外,请在应用程序提供的DefaultConnection之外,将正确的连接字符串添加到配置文件中。我希望这会有所帮助
谢谢如果您命名列Id而不是用户Id,它可以自动找到您的主键,但就我个人而言,我从未使用过它,只是为了让您知道这非常有效,但现在我在数据库“master”中遇到以下错误:创建数据库权限被拒绝。