An exception of type 'System.Data.Entity.Core.MappingException' occurred in EntityFramework.dll but was not handled in user code

asp.net c# db-first entity-framework-6

Question

I am trying to insert data using this function in entity framework but without 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");

        }
    }

Additional information about this exception is :

"The type 'Edm.String' of the member 'SchoolId' in the conceptual side type 'DatabaseDBModel.student' does not match with the type 'System.Int32' of the member 'SchoolId' on the object side type 'SchoolManagementSystemOOP.Models.student'."

My branch class code is as follows :

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

}

The student class code:

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


}

I want some insight and clues about what is causing the exception, I have also tried to change the type int of school with that of string.

1
0
10/19/2017 12:44:37 PM

Popular Answer

in student class you want schoolId type is int but in brunch class it declared as string. if they are using for mapping anywhere ef cant generate this. You can try in product class.

public string schoolId{get;set;}

to

public int schoolId{get;set};
0
10/19/2017 12:53:03 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow