繁体   English   中英

ASP.Net MVC无法为SQL Server数据库创建控制器

[英]ASP.Net MVC cannt create controller for sql server db

当我尝试为sql server表而不是代码优先(localdb)创建控制器时,我发现了问题,但是我检查了connectionString,但是我认为我没有犯错

为了安全起见,我隐藏了机器名称,用户名和密码

在模型生成期间检测到一个或多个验证错误:

DB2Ex.Model.Student::EntityType'Student'没有定义键。 定义此EntityType的键。

学生::EntityType'学生'尚未定义密钥。 定义此EntityType的键。

public class Student
{
    public int xh { get; set; }
    public string xm { get; set; }
    public string xb { get; set; }
    public string csrq { get; set; }
    public string jg { get; set; }
    public string sjhm { get; set; }
    public string yxh { get; set; }
    public string mm { get; set; }
}
public class StudentDBContext : DbContext
{
    public StudentDBContext()
        : base("schoolDB")
    {
    }
    public DbSet<Student> Students { get; set; }
}

<add name="schoolDB" connectionString="Data Source=DESKTOP-*****;Initial Catalog=school;Persist Security Info=True;User ID=***;Password=***" providerName="System.Data.SqlClient" />

假设xh是数据库中的关键字段,那么您也需要在Student模型中对其进行定义:

public class Student
{
    [Key]
    public int xh { get; set; }
    public string xm { get; set; }
    public string xb { get; set; }
    public string csrq { get; set; }
    public string jg { get; set; }
    public string sjhm { get; set; }
    public string yxh { get; set; }
    public string mm { get; set; }
}

我找到解决方法

-Models
-Create a "ADO.Net Model"
-Choose the sql server db
-Dont forget choose "create the default connectionstring"

然后为VS创建的db.tables和Dbcontext创建一个控制器

我将得到类似的观点,例如选择代码优先的方式:创建新的,细节,删除和其他选项

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM