Задача
У меня есть три модельных класса: студент, работа и выпускники. Сначала я создал EF DB, и мне удалось сохранить сведения об выпускниках, но при сохранении сведений об ученике и работе я столкнулся с ошибкой. Прикрепил файл edmx отношения для справки.
Выпуск:
Я сталкиваюсь с ошибкой типа «Исключение типа« System.Data.Entity.Validation.DbEntityValidationException »возникло в EntityFramework.dll, но не было обработано в коде пользователя» при создании новой записи для Job и Student.
Результат:
Мне нужно сохранить новые записи для рабочих мест и студентов из EF, я смог сохранить данные выпускников
Пояснение:
Есть ли способ проверить, выполняется ли запрос к серверу SQL из Visual Studio в платформе сущностей, кроме профилировщика
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