[英]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.