第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

System.Data.SqlClient.SqlException:列名“Gender_id”無效

System.Data.SqlClient.SqlException:列名“Gender_id”無效

C#
慕慕森 2022-01-09 15:02:30
我正在學(xué)習(xí)使用 asp.net mvc 和實(shí)體框架進(jìn)行 Web 應(yīng)用程序開發(fā)。我有一個(gè)包含 3 個(gè)學(xué)生、性別和項(xiàng)目表的數(shù)據(jù)庫。我遇到了這個(gè)問題。錯(cuò)誤在第 23 行。列名“Genders_Id”無效。列名“Programs_Id”無效。異常詳細(xì)信息: System.Data.SqlClient.SqlException:列名“Genders_Id”無效。列名“Programs_Id”無效。Source Error:Line 21:         {Line 22:             StudentsContext stdContext = new StudentsContext();Line 23:             Students students = stdContext.Students.Single(std => std.Id == id);Line 24:             //ViewBag.Student = students;Line 25:             return View(students);我的 Students.cs 模型代碼:namespace WebApplication4.Models{  public class Students  {     public string Name { get; set; }     public int Id { get; set; }     public int Gender { get; set; }     public int Program { get; set; }     public string Country { get; set; }   }}我的 Context.cs 文件:public class StudentsContext:DbContext{    public DbSet<Students> Students { get; set; }    public DbSet<Programs> Programs { get; set; }    public DbSet<Genders> Genders { get; set; }}程序.cs:public class Programs{    public int Id { get; set; }    public string Program { get; set; }    public List<Students> Students { get; set; }}性別.cs:public class Genders{    public int Id { get; set; }    public string Gender { get; set; }    public List<Students> Students { get; set; }}數(shù)據(jù)庫表:
查看完整描述

2 回答

?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

Genders_Id和Programs_Id是 FK 列的默認(rèn) EF6 常規(guī)名稱,用于分別由Genders.Students和Programs.Students集合導(dǎo)航屬性引入的一對多關(guān)系。


由于您的 FK 屬性/列名稱不是傳統(tǒng)的,您應(yīng)該通過[ForeinKey]數(shù)據(jù)注釋來指定它們:


public class Programs

{

    // ...

    [ForeignKey("Program")]

    public List<Students> Students { get; set; }

}


public class Genders

{

    // ...

    [ForeignKey("Gender")]

    public List<Students> Students { get; set; }

}

或(我的首選)流利的 API:


protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

    // ...


    modelBuilder.Entity<Genders>()

        .HasMany(e => e.Students)

        .WithRequired()

        .HasForeignKey(e => e.Gender);


    modelBuilder.Entity<Programs>()

        .HasMany(e => e.Students)

        .WithRequired()

        .HasForeignKey(e => e.Program);

}


查看完整回答
反對 回復(fù) 2022-01-09
?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

您的問題是數(shù)據(jù)層中的連接字符串和 Web 層中的連接字符串指向不同的數(shù)據(jù)庫。

例如,數(shù)據(jù)層讀取指向測試數(shù)據(jù)庫的開發(fā)數(shù)據(jù)庫 webapp。

要么更新連接字符串以指向同一個(gè)數(shù)據(jù)庫

或者

確保您的兩個(gè)數(shù)據(jù)庫具有相同的表和列


查看完整回答
反對 回復(fù) 2022-01-09
  • 2 回答
  • 0 關(guān)注
  • 361 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號