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
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. Define the key for this EntityType.
Students: : EntityType 'Students' has no key defined. Define the key for this 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:
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
And i will get the similar views like choosing the code-first way:create new,details,delete and other options
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.