简体   繁体   English

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

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

i find the problem when i tried to create a controller for sql server table,not code-first(localdb),and i check the connectionString ,but i think i hasn't mistakes on it 当我尝试为sql server表而不是代码优先(localdb)创建控制器时,我发现了问题,但是我检查了connectionString,但是我认为我没有犯错

for safety ,i hide the machine-name,username and password 为了安全起见,我隐藏了机器名称,用户名和密码

One or more validation errors were detected during model generation: 在模型生成期间检测到一个或多个验证错误:

DB2Ex.Model.Student: : EntityType 'Student' has no key defined. DB2Ex.Model.Student::EntityType'Student'没有定义键。 Define the key for this EntityType. 定义此EntityType的键。

Students: : EntityType 'Students' has no key defined. 学生::EntityType'学生'尚未定义密钥。 Define the key for this 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" />

Assuming xh is the key field in the database, you need to define it in your Student model too: 假设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; }
}

I find the way to solve it 我找到解决方法

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

Then create a controller for the db.tables and the Dbcontext that hava been created by VS 然后为VS创建的db.tables和Dbcontext创建一个控制器

And i will get the similar views like choosing the code-first way:create new,details,delete and other options 我将得到类似的观点,例如选择代码优先的方式:创建新的,细节,删除和其他选项

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

相关问题 Controller中的ASP.NET MVC实体框架DB访问 - ASP.NET MVC Entity Framework DB access in Controller ASP.NET MVC 4-如何在删除旧项目的同时将SQL Server Express DB移至新项目? - ASP.NET MVC 4 - How to move SQL Server Express DB to new project while deleting old project? ASP.NET MVC 4:对 controller 进行 Ajax 调用以更新 Z9778840A0100CB30C982228B7 数据库时出错 - ASP.NET MVC 4: error when making Ajax call to controller to update SQL Server database ASP.NET MVC 使用 WCF 从 SQL Server 获取数据:如何在控制器中放置临时数据存储 - ASP.NET MVC get data from SQL Server with WCF : how to put temporary data store in controller ASP.NET MVC 数据不会保存在 SQL 数据库中 - ASP.NET MVC Data won't be saved in SQL db ASP.NET MVC DB首先创建动态dbcontext - ASP.NET MVC DB First create dynamic dbcontext 在ASP.NET MVC中创建数据库回调的最有效方法 - Most Efficient Way to Create a DB Callback in ASP.NET MVC 如何使用db查询在ASP.NET MVC 5中创建复杂的EditorTemplate? - How create a complex EditorTemplate in ASP.NET MVC 5 with db query? 在ASP.NET MVC 3支架中不允许访问SQL Compact 4 DB - Access to sql compact 4 db not allowed in ASP.NET MVC 3 Scaffolding 如何在ASP.NET MVC中创建控制器来处理URL - How to create controller to handle the url in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM