2017-10-19 70 views
0

我想在實體框架,但沒有MVC的類型「System.Data.Entity.Core.MappingException」的異常出現在EntityFramework.dll但在用戶代碼

 protected void SaveBranch_Click(object sender, EventArgs e) 
    { 
     if (DropDownBranchSchool.Text == string.Empty) 
     { 
     } 
     else 
     { 

      branch newBranch = new branch(txtBoxBranchName.Text, txtBoxBranchLocation.Text, txtBoxBranchPhone.Text, txtBoxBranchEmail.Text, Int32.Parse(DropDownBranchSchool.Text)); 
      _SchoolDBEntity.branches.Add(newBranch); 
      _SchoolDBEntity.SaveChanges(); 
      Response.Redirect("Default.aspx"); 

     } 
    } 
使用該功能來插入數據沒有處理

有關此異常的附加信息爲:

「的類型在概念側類型‘DatabaseDBModel.student’的成員‘SchoolId’不與類型‘System.Int32’的匹配的‘Edm.String’成員'SchoolId'在對象端類型'SchoolManagementSystemOOP.Models.student'。「

支行類的代碼如下:

   public partial class branch 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public branch(string name, string location, string phone, string email, int schoolId) 
    { 
     this.students = new HashSet<student>(); 
     BName = name; 
     BLocation = location; 
     BPhone = phone; 
     BEmailID = email; 
     SchoolId = schoolId; 

    } 

    public int BId { get; set; } 
    public string BName { get; set; } 
    public string BLocation { get; set; } 
    public string BPhone { get; set; } 
    public string BEmailID { get; set; } 
    public int SchoolId { get; set; } 

    public virtual school school { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<student> students { get; set; } 
} 

}

學生類代碼:

   public partial class student 
{ 
    public int Id { get; set; } 
    public string FName { get; set; } 
    public string LName { get; set; } 
    public string Phone { get; set; } 
    public string EmailId { get; set; } 
    public string SchoolId { get; set; } 
    public string BranchId { get; set; } 

    public virtual branch branch { get; set; } 

    public student(string fname, string lname, string phone, string email, string schoolId, string branchId) 
    { 
     FName = fname; 
     LName = lname; 
     Phone = phone; 
     EmailId = email; 
     SchoolId = schoolId; 
     BranchId = branchId; 
    } 


} 

我想了解是什麼導致了異常的一些見解和線索,我也試圖用字符串的類型來改變學校的類型。

+0

請加上學生類 –

回答

0
學生類

你想schoolId類型爲int,但在早午餐類它宣佈爲字符串。如果他們正在使用映射任何地方,如果不能產生這一點。您可以在產品類別中嘗試 。

public string schoolId{get;set;} 

public int schoolId{get;set}; 
相關問題