EntityFramework.dll 中出现“System.Data.Entity.Core.MappingException”类型的异常,但未在用户代码中处理
Posted
技术标签:
【中文标题】EntityFramework.dll 中出现“System.Data.Entity.Core.MappingException”类型的异常,但未在用户代码中处理【英文标题】:An exception of type 'System.Data.Entity.Core.MappingException' occurred in EntityFramework.dll but was not handled in user code 【发布时间】:2018-03-31 11:29:00 【问题描述】:我正在尝试在实体框架中使用此函数插入数据,但没有 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'与对象端类型'中成员'SchoolId'的类型'System.Int32'不匹配' 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;
我想要一些关于是什么导致异常的见解和线索,我还尝试将学校的 int 类型更改为 string 的类型。
【问题讨论】:
请添加学生班级 【参考方案1】:在学生类中,您希望 schoolId 类型为 int,但在早午餐类中,它声明为字符串。如果他们用于在任何地方进行映射,则无法生成此内容。你可以试试 在产品类中。
public string schoolIdget;set;
到
public int schoolIdget;set;
【讨论】:
以上是关于EntityFramework.dll 中出现“System.Data.Entity.Core.MappingException”类型的异常,但未在用户代码中处理的主要内容,如果未能解决你的问题,请参考以下文章
正在编译转换: 未能找到元数据文件 EntityFramework.dll