I have a model named AcademicLevel
:
public class EducationLevel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public virtual ClassName ClassName { get; set; }
public virtual SectionName SectionName { get; set; }
public int Years { get; set; }
public virtual Gender Genders { get; set; }
public virtual MediumName Medium { get; set; }
public int MaxCapacity { get; set; }
public virtual WorkStation Campus { get; set; }
public virtual AcademicSession AcSession { get; set; }
public virtual ICollection<Student>? Students { get; set; }
}
The model class for WorkStation
is
public class WorkStation
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int? Id { get; set; }
public string Name { get; set; }
[Required]
public virtual Company? Company { get; set; }
[Required]
public string Address { get; set; } = string. Empty;
public virtual ICollection<EducationLevel>? Classes { get; set; }
public virtual ICollection<ApplicationUser>? AppUsers { get; set; }
}
I am using a Windows UserControl to get the values of all the properties on the click event of a button.
EducationLevel cls = new EducationLevel()
{
MaxCapacity = Convert.ToInt32(MaxCapacitySpinEdit.EditValue),
Years = Convert.ToInt32(YearsSpinEdit.EditValue)
};
cls.ClassName = (ClassName)classNameBindingSource.Current;
cls.SectionName = (SectionName)sectionNameBindingSource.Current;
cls.Genders = (Gender)genderBindingSource.Current;
cls.Medium = (MediumName)mediumNameBindingSource.Current;
cls.Campus = Behaviour.LoginInfo.User.Office;
cls.AcSession = _context.AcSessions.Where(s => s.Id == 1).FirstOrDefault();
_context.Classes.Add(cls);
_context.SaveChanges();
The CREATE TABLE
for AcademicLevel
or cls
is below:
USE [AIMS_db1]
GO
/****** Object: Table [dbo].[Classes] Script Date: 10/23/2023 12:02:07 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Classes]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[ClassNameId] INT NOT NULL,
[SectionNameId] INT NOT NULL,
[Years] INT NOT NULL,
[GendersId] INT NOT NULL,
[MediumId] INT NOT NULL,
[MaxCapacity] INT NOT NULL,
[CampusId] INT NOT NULL,
[AcSessionId] INT NOT NULL,
[EntryTime] DATETIME2 (7) NULL,
[CreatedByUser] NVARCHAR (MAX) NULL,
[LasteModifiedTime] DATETIME2 (7) NULL,
[LastModifiedByUser] NVARCHAR (MAX) NULL,
[Version] INT NOT NULL
);
GO
CREATE NONCLUSTERED INDEX [IX_Classes_AcSessionId]
ON [dbo].[Classes]([AcSessionId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Classes_CampusId]
ON [dbo].[Classes]([CampusId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Classes_ClassNameId]
ON [dbo].[Classes]([ClassNameId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Classes_GendersId]
ON [dbo].[Classes]([GendersId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Classes_MediumId]
ON [dbo].[Classes]([MediumId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Classes_SectionNameId]
ON [dbo].[Classes]([SectionNameId] ASC);
GO
ALTER TABLE [dbo].[Classes]
ADD CONSTRAINT [PK_Classes] PRIMARY KEY CLUSTERED ([Id] ASC);
GO
ALTER TABLE [dbo].[Classes]
ADD CONSTRAINT [FK_Classes_AcSessions_AcSessionId]
FOREIGN KEY ([AcSessionId]) REFERENCES [dbo].[AcSessions] ([Id]);
GO
ALTER TABLE [dbo].[Classes]
ADD CONSTRAINT [FK_Classes_ClassNames_ClassNameId]
FOREIGN KEY ([ClassNameId]) REFERENCES [dbo].[ClassNames] ([Id]);
GO
ALTER TABLE [dbo].[Classes]
ADD CONSTRAINT [FK_Classes_Genders_GendersId]
FOREIGN KEY ([GendersId]) REFERENCES [dbo].[Genders] ([Id]);
GO
ALTER TABLE [dbo].[Classes]
ADD CONSTRAINT [FK_Classes_Mediums_MediumId]
FOREIGN KEY ([MediumId]) REFERENCES [dbo].[Mediums] ([Id]);
GO
ALTER TABLE [dbo].[Classes]
ADD CONSTRAINT [FK_Classes_SectionNames_SectionNameId]
FOREIGN KEY ([SectionNameId]) REFERENCES [dbo].[SectionNames] ([Id]);
GO
ALTER TABLE [dbo].[Classes]
ADD CONSTRAINT [FK_Classes_WorkStations_CampusId]
FOREIGN KEY ([CampusId]) REFERENCES [dbo].[WorkStations] ([Id]);
I get an error
Cannot insert explicit value for identity column in table
WorkStations
whenIDENTITY_INSERT
is set toOFF
While I am not trying to save WorkStations
but EducationLevel
only, why do I get this unrelated error?