我有一个奇怪的情况,我的数据库中的一些表从0开始其ID,即使TABLE CREATE具有IDENTITY(1,1).某些表格是这样,但其他表格则不然.它一直工作到今天.
我已经尝试重置标识列:
DBCC CHECKIDENT (SyncSession, reseed, 0);
Run Code Online (Sandbox Code Playgroud)
但是新的记录从0开始.我已经尝试过为所有表做这个,但有些仍然从0开始,有些从1开始.
有什么指针吗?
(我正在使用带有高级服务的SQL Server Express 2005)
基本上我需要将所有表的Identity Increment重置为原始表.在这里我尝试了一些代码,但它失败了.
来自链接的实际代码:
USE World00_Character
GO
-- Create a cursor to loop through the System Ojects and get each table name
DECLARE TBL_CURSOR CURSOR
-- Declare the SQL Statement to cursor through
FOR ( SELECT Name FROM Sysobjects WHERE Type='U' )
-- Declare the @SQL Variable which will hold our dynamic sql
DECLARE @SQL NVARCHAR(MAX);
SET @SQL = '';
-- Declare the @TblName Variable which will hold the name of the current table
DECLARE @TblName NVARCHAR(MAX);
-- Open the …Run Code Online (Sandbox Code Playgroud) 有这两个实体:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public CompanyVehicle CompanyVehicle { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class CompanyVehicle
{
public int Id { get; set; }
public string Name { get; set; }
public Employee Employee { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用Entity Framework Core 5.0.8on SQL Server 2019,CompanyVehicle 的配置为:
entityBuilder.HasOne(t => t.Employee)
.WithOne(t => t.CompanyVehicle)
.HasForeignKey<Employee>(t => t.Id)
.IsRequired();
Run Code Online (Sandbox Code Playgroud)
我们将尝试插入一些东西:
public void Create(Employee employee) …Run Code Online (Sandbox Code Playgroud) 全部,我想IDENTITY根据从另一个表获得的当前最大值开始字段的编号.所以我尝试了类似下面的内容
DECLARE @CurrentES INT;
SET @CurrentES = (SELECT MaxES
FROM [NDB]..[TmpMaxES]) + 1;
ALTER TABLE BA
ADD ES INT IDENTITY(@CurrentES, 1);
Run Code Online (Sandbox Code Playgroud)
但是这不会接受变量作为IDENTITY中的种子值.我怎样才能实现我的目标?
谢谢你的时间.