Entity framework core C 35;如何在上下文文件中的数据库上执行npsql命令,而不是使用_context.Database.ExecuteSqlCommand for SQL
我有一个C#项目,它使用两个提供者:SQL和POSTGRESS 我必须在DB上运行一个命令来打开和关闭IDENTITY_INSERT 我知道如何在SQL中执行此操作:Entity framework core C 35;如何在上下文文件中的数据库上执行npsql命令,而不是使用_context.Database.ExecuteSqlCommand for SQL,entity-framework-core,npgsql,Entity Framework Core,Npgsql,我有一个C#项目,它使用两个提供者:SQL和POSTGRESS 我必须在DB上运行一个命令来打开和关闭IDENTITY_INSERT 我知道如何在SQL中执行此操作: _context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].CustomerType ON"); 但是,我如何对POSTGRESS做同样的事情呢 我想的是: NpgsqlCommand command = new NpgsqlCommand(
_context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].CustomerType ON");
但是,我如何对POSTGRESS做同样的事情呢
我想的是:
NpgsqlCommand command = new NpgsqlCommand(@"SET IDENTITY_INSERT [dbo].CustomerType ON");
但我不确定这是不是一种方法
以下是我的线路代码:
var strategy = _context.Database.CreateExecutionStrategy();
strategy.Execute(() =>
{
using (var transaction = _context.Database.BeginTransaction())
{
bool isIsNpgsql = _context.Database.IsNpgsql();
_context.CustomerType.Add(ManagementCustomerType);
if (isIsNpgsql)
{
NpgsqlCommand command = new NpgsqlCommand(@"SET IDENTITY_INSERT [dbo].CustomerType ON");
_context.SaveChanges();
NpgsqlCommand command = new NpgsqlCommand(@"SET IDENTITY_INSERT [dbo].CustomerType OFF");
}
else
{
_context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].CustomerType ON");
_context.SaveChanges();
_context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].CustomerType OFF");
}
transaction.Commit();
}
});
所有的帮助都将得到评估 只需插入您的数据。你所需要的只是序列。查看此链接了解更多详细信息。谢谢你把我引向了正确的方向!