提问人:Fayyaz Ahmed Nadeem 提问时间:10/23/2023 最后编辑:marc_sFayyaz Ahmed Nadeem 更新时间:10/23/2023 访问量:60
当 SaveChanges 时,无法为标识列插入显式值
Cannot insert explicit value for identity column when SaveChanges
问:
我有一个名为: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; }
}
的模型类是WorkStation
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; }
}
我正在使用 Windows UserControl 来获取按钮的单击事件上所有属性的值。
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();
for 或如下:CREATE TABLE
AcademicLevel
cls
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]);
我收到错误
当设置为
WorkStations
IDENTITY_INSERT
OFF
虽然我不是在尝试保存,而只是尝试保存,但为什么我会遇到这个不相关的错误?WorkStations
EducationLevel
答: 暂无答案
评论
CREATE TABLE
Convert.ToInt32
<-- 请不要将该类用于任何用途(除了 Base64 转换),因为它是 .NET Framework 1.1(大约 2002 年)的遗留物,旨在供 VB.NET 用户使用(具有可怕的默认值和不合理的故障模式......相反,您应该使用 or - 不要忘记始终设置显式以确保一致的解析。Convert
Int32.TryParse
Int32.Parse
CultureInfo
WorkStations
WorkStation
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]