问题
我有学生,求职和校友三个模型班。我创建了EF DB的第一个方法,能够保存校友详细信息,但是在保存学生和工作详细信息时遇到了错误。附加了关系edmx文件以供参考。
问题: 清除作业和学生的新记录时,我遇到诸如“ EntityFramework.dll中发生类型'System.Data.Entity.Validation.DbEntityValidationException的异常,但未在用户代码中处理”的错误。
结果: 我需要保存来自EF的工作和学生的新记录,我能够保存校友详细信息
说明: 除了探查器之外,是否有其他方法可以检查从实体框架中的Visual Studio对SQL Server执行的查询
JobController:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="JCompanyName,JobType,JDesignation,JElgibility,JLocation,JYearOfPassed,JPercentReq,JDescription,JAddress,JSalary,JLastDateToApply,JNumberOfVaccancy,JHiringRounds,IsActive,IsCompany")] Job job)
{
if (ModelState.IsValid)
{
db.Jobs.Add(job);
db.SaveChanges(); //Facing the issue here for jobs and Student
//controller
return RedirectToAction("Index");
}
ViewBag.Alumni_ID = new SelectList(db.Alumni, "Id", "AName", job.Alumni_ID);
return View(job);
}
工作模式:
public partial class Job
{
public Job()
{
this.Students = new HashSet<Student>();
}
public int Id { get; set; }
public int Alumni_ID { get; set; }
public string JCompanyName { get; set; }
public string JobType { get; set; }
public string JDesignation { get; set; }
public string JElgibility { get; set; }
public string JLocation { get; set; }
public int JYearOfPassed { get; set; }
public int JPercentReq { get; set; }
public string JDescription { get; set; }
public string JAddress { get; set; }
public string JSalary { get; set; }
public System.DateTime JLastDateToApply { get; set; }
public int JNumberOfVaccancy { get; set; }
public string JHiringRounds { get; set; }
public Nullable<bool> IsActive { get; set; }
public Nullable<bool> IsCompany { get; set; }
public virtual Alumnus Alumnus { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
学生模型:
public partial class Student
{
public int Id { get; set; }
public int Job_Id { get; set; }
public string Sname { get; set; }
public string Sphone { get; set; }
public string SEmail { get; set; }
public string SAddress { get; set; }
public string SchoolName { get; set; }
public string StudyType { get; set; }
public string CollegeOrBoard { get; set; }
public decimal SchoolPercent { get; set; }
public string SchoolState { get; set; }
public string SchoolCity { get; set; }
public string Skills { get; set; }
public string SkillLevel { get; set; }
public string IntermediateName { get; set; }
public string IntermediateStudyType { get; set; }
public string IntermediateBoard { get; set; }
public string IntermediatePercent { get; set; }
public string IntermediateState { get; set; }
public string IntermediateCity { get; set; }
public string UGName { get; set; }
public string UGStream { get; set; }
public string UGStudyType { get; set; }
public string UGBoard { get; set; }
public string UGPercent { get; set; }
public string UGState { get; set; }
public string UGCity { get; set; }
public virtual Job Job { get; set; }
}
校友模型:
public partial class Alumnus
{
public Alumnus()
{
this.Jobs = new HashSet<Job>();
}
public int Id { get; set; }
public string AName { get; set; }
public string ACompany { get; set; }
public string APhone { get; set; }
public string AEmail { get; set; }
public string AAddress { get; set; }
public string ACompanyAddress { get; set; }
public int ABatchYear { get; set; }
public virtual ICollection<Job> Jobs { get; set; }
}
SQL脚本:
USE [AlumniConnect]
GO
/****** Object: Table [dbo].[Alumni] Script Date: 05/04/2019 21:57:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Alumni](
[Id] [int] IDENTITY(1,1) NOT NULL,
[AName] [varchar](500) NOT NULL,
[ACompany] [varchar](500) NOT NULL,
[APhone] [varchar](500) NOT NULL,
[AEmail] [varchar](500) NOT NULL,
[AAddress] [varchar](500) NOT NULL,
[ACompanyAddress] [varchar](500) NOT NULL,
[ABatchYear] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Jobs] Script Date: 05/04/2019 21:57:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Jobs](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Alumni_ID] [int] NOT NULL,
[JCompanyName] [varchar](1) NOT NULL,
[JobType] [varchar](1) NOT NULL,
[JDesignation] [varchar](1) NOT NULL,
[JElgibility] [varchar](1) NOT NULL,
[JLocation] [varchar](1) NOT NULL,
[JYearOfPassed] [int] NOT NULL,
[JPercentReq] [int] NOT NULL,
[JDescription] [varchar](max) NOT NULL,
[JAddress] [varchar](max) NOT NULL,
[JSalary] [varchar](max) NOT NULL,
[JLastDateToApply] [date] NOT NULL,
[JNumberOfVaccancy] [int] NOT NULL,
[JHiringRounds] [varchar](max) NOT NULL,
[IsActive] [bit] NULL,
[IsCompany] [bit] NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Student] Script Date: 05/04/2019 21:57:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Job_Id] [int] NOT NULL,
[Sname] [varchar](1) NOT NULL,
[Sphone] [varchar](15) NOT NULL,
[SEmail] [varchar](50) NOT NULL,
[SAddress] [varchar](max) NOT NULL,
[SchoolName] [varchar](max) NOT NULL,
[StudyType] [varchar](50) NOT NULL,
[CollegeOrBoard] [varchar](max) NOT NULL,
[SchoolPercent] [decimal](18, 0) NOT NULL,
[SchoolState] [varchar](200) NOT NULL,
[SchoolCity] [varchar](200) NOT NULL,
[Skills] [varchar](500) NOT NULL,
[SkillLevel] [varchar](1) NOT NULL,
[IntermediateName] [varchar](200) NOT NULL,
[IntermediateStudyType] [varchar](500) NOT NULL,
[IntermediateBoard] [varchar](500) NOT NULL,
[IntermediatePercent] [varchar](500) NOT NULL,
[IntermediateState] [varchar](500) NOT NULL,
[IntermediateCity] [varchar](500) NOT NULL,
[UGName] [varchar](500) NOT NULL,
[UGStream] [varchar](500) NOT NULL,
[UGStudyType] [varchar](500) NOT NULL,
[UGBoard] [varchar](500) NOT NULL,
[UGPercent] [varchar](500) NOT NULL,
[UGState] [varchar](500) NOT NULL,
[UGCity] [varchar](500) NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[SEmail] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[Sphone] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: ForeignKey [Jobs_fk0] Script Date: 05/04/2019 21:57:36 ******/
ALTER TABLE [dbo].[Jobs] WITH CHECK ADD CONSTRAINT [Jobs_fk0] FOREIGN KEY([Alumni_ID])
REFERENCES [dbo].[Alumni] ([Id])
GO
ALTER TABLE [dbo].[Jobs] CHECK CONSTRAINT [Jobs_fk0]
GO
/****** Object: ForeignKey [Student_fk0] Script Date: 05/04/2019 21:57:36 ******/
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [Student_fk0] FOREIGN KEY([Job_Id])
REFERENCES [dbo].[Jobs] ([Id])
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [Student_fk0]
GO
答案 0 :(得分:0)
这里的保存方式很重要,因为数据库依赖
尝试一下
[HttpPost]
[ValidateAntiForgeryToken]
//I removed the binding to be clearer, you can still include it but you need to pass the models. I don't know if you have passed them before
public ActionResult Create(Job job, Student student, Alumni alumni)
{
if (ModelState.IsValid)
{
//you will have to save in this pattern Alumni first, Job second, Student last
db.Alumni.Add(alumni);
db.SaveChanges();
//save the job
job.Alunmi_ID = alumni.Id
db.Job.Add(job)
db.SaveChanges()
//save the student
student.Job_ID = job.Id
db.Student.Add(student)
db.SaveChanges()
return RedirectToAction("Index");
}
ViewBag.Alumni_ID = new SelectList(db.Alumni, "Id", "AName", job.Alumni_ID);
return View(job);
}