0

我正在尝试在实体框架中使用此函数插入​​数据但没有 MVC

       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”的类型“Edm.String”与对象端类型“SchoolManagementSystemOOP.Models”中成员“SchoolId”的类型“System.Int32”不匹配。学生'。”

我的分支类代码如下:

               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;
    }


}

我想要一些有关导致异常的原因的见解和线索,我还尝试将学校的 int 类型更改为 string 的类型。

4

1 回答 1

0

在学生类中,您希望 schoolId 类型为 int,但在早午餐类中,它声明为字符串。如果他们用于在任何地方进行映射,则无法生成此内容。您可以在产品类中尝试。

public string schoolId{get;set;}

public int schoolId{get;set};
于 2017-10-19T12:53:03.490 回答