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