gpt4 book ai didi

c# - 添加 Controller 时无法获取元数据mvc4

转载 作者:行者123 更新时间:2023-11-30 15:03:09 26 4
gpt4 key购买 nike

我正在尝试创建我的第一个 mvc 类,但在开始时遇到了问题。我在我的托管帐户上使用共享数据库并创建了以下类

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; }
}
}

这是我的连接字符串

   <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" />

我得到的错误是

Unable to retrieve metadata for 'mvcapplication2.models.tusers' one or more validation errors were detected during model generation:

Entity type users has no defended key.

另外,我的模型名称是否需要与我的表格名称完全匹配?

最佳答案

Entity Framework 无法识别您的 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; }
}

关于c# - 添加 Controller 时无法获取元数据mvc4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11945623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com