Entity framework 和 sql 身份问题
Entity framework and sql identity issue
我需要重置我的 table ID(身份),因为每次更新时,我的 ID 都必须从 1 开始,因为他们每次都会将其增加到超过 60 000 条记录.我该怎么做?
using (DailyContext context= DailyContext.Create())
{
//cleaning old prices
foreach (var price in context.Prices)
{
context.DeleteObject(price);
}
context.SaveChanges();
for (int i = 0; i < newElements.Total; i++)
{
var newPrice = new Price()
{
Date = newElements.From.AddDays(i),
PriceFrom = newElements.Price,
TotalNights = newElements.TotalNights,
Profit = newElements.Profit
};
context.AddToPrices(newPrice);
}
context.SaveChanges();
}
要重置身份,您需要在 EF 之外进行,并且必须进行原始查询。您需要 运行 的查询是带有 RESEED
参数的 DBCC CHECKIDENT
。
using (DailyContext context= DailyContext.Create())
{
//cleaning old prices
foreach (var price in context.Prices)
{
context.DeleteObject(price);
}
context.SaveChanges();
//Reseed the identity to 0.
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT (Prices, RESEED, 0)");
//Roll the identity forward till it finds the last used number.
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT (Prices, RESEED)");
for (int i = 0; i < newElements.Total; i++)
{
var newPrice = new Price()
{
Date = newElements.From.AddDays(i),
PriceFrom = newElements.Price,
TotalNights = newElements.TotalNights,
Profit = newElements.Profit
};
context.AddToPrices(newPrice);
}
context.SaveChanges();
}
编辑:
这是一个仅使用 SqlCommand 执行的版本,不依赖于 Entity Framework。
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
using (SqlTransaction trans = connection.BeginTransaction("PricesCleanup"))
using (var command = new SqlCommand("", connection, trans))
{
//Reseed the identity to 0.
command.CommandText = "DBCC CHECKIDENT (Prices, RESEED, 0)";
command.ExecuteNonQuery();
//Roll the identity forward till it finds the last used number.
command.CommandText = "DBCC CHECKIDENT (Prices, RESEED)";
command.ExecuteNonQuery();
trans.Commit();
}
}
不要在 EF 中单独循环和删除价格,只需使用 ExecuteSqlCommand
到 TRUNCATE
table。
这既清空了它(比 DELETE
更有效)又重置了 IDENTITY
列值。
我需要重置我的 table ID(身份),因为每次更新时,我的 ID 都必须从 1 开始,因为他们每次都会将其增加到超过 60 000 条记录.我该怎么做?
using (DailyContext context= DailyContext.Create())
{
//cleaning old prices
foreach (var price in context.Prices)
{
context.DeleteObject(price);
}
context.SaveChanges();
for (int i = 0; i < newElements.Total; i++)
{
var newPrice = new Price()
{
Date = newElements.From.AddDays(i),
PriceFrom = newElements.Price,
TotalNights = newElements.TotalNights,
Profit = newElements.Profit
};
context.AddToPrices(newPrice);
}
context.SaveChanges();
}
要重置身份,您需要在 EF 之外进行,并且必须进行原始查询。您需要 运行 的查询是带有 RESEED
参数的 DBCC CHECKIDENT
。
using (DailyContext context= DailyContext.Create())
{
//cleaning old prices
foreach (var price in context.Prices)
{
context.DeleteObject(price);
}
context.SaveChanges();
//Reseed the identity to 0.
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT (Prices, RESEED, 0)");
//Roll the identity forward till it finds the last used number.
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT (Prices, RESEED)");
for (int i = 0; i < newElements.Total; i++)
{
var newPrice = new Price()
{
Date = newElements.From.AddDays(i),
PriceFrom = newElements.Price,
TotalNights = newElements.TotalNights,
Profit = newElements.Profit
};
context.AddToPrices(newPrice);
}
context.SaveChanges();
}
编辑:
这是一个仅使用 SqlCommand 执行的版本,不依赖于 Entity Framework。
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
using (SqlTransaction trans = connection.BeginTransaction("PricesCleanup"))
using (var command = new SqlCommand("", connection, trans))
{
//Reseed the identity to 0.
command.CommandText = "DBCC CHECKIDENT (Prices, RESEED, 0)";
command.ExecuteNonQuery();
//Roll the identity forward till it finds the last used number.
command.CommandText = "DBCC CHECKIDENT (Prices, RESEED)";
command.ExecuteNonQuery();
trans.Commit();
}
}
不要在 EF 中单独循环和删除价格,只需使用 ExecuteSqlCommand
到 TRUNCATE
table。
这既清空了它(比 DELETE
更有效)又重置了 IDENTITY
列值。