【发布时间】:2019-02-04 07:44:42
【问题描述】:
我有一个从序列生成主键的表:
CREATE SEQUENCE [dbo].[seq_PK_testTable] AS [int] START WITH 0 INCREMENT BY 1;
CREATE TABLE [dbo].[testTable](
[id] [int] NOT NULL,
CONSTRAINT [PK_testTable] PRIMARY KEY CLUSTERED ([id] ASC)) ON [PRIMARY];
ALTER TABLE [dbo].[testTable] ADD CONSTRAINT [DF_testTable_id] DEFAULT (NEXT VALUE FOR [seq_PK_testTable]) FOR [id];
为了能够使用此密钥生成机制,我选择使用数据上下文的 Insert 部分方法,正如 this answer 中所建议的那样。
对于第一个插入,它就像一个魅力:执行插入并在对象中更新 ID,但是当我插入第二个对象时,我得到一个 System.Data.Linq.DuplicateKeyException: 'Cannot add an entity with a key that is already in use.'。
MCVE:
using System.Data.Linq.Mapping;
namespace test
{
static class Program
{
static void Main(string[] args)
{
var db = new DataClasses1DataContext(@"Data Source=");
var testTableRecord1 = new testTable();
var testTableRecord2 = new testTable();
db.GetTable<testTable>().InsertOnSubmit(testTableRecord1);
db.SubmitChanges();
db.GetTable<testTable>().InsertOnSubmit(testTableRecord2);
db.SubmitChanges();
}
}
[Database(Name = "TestDB")]
public class DataClasses1DataContext : System.Data.Linq.DataContext
{
public DataClasses1DataContext(string fileOrServerOrConnection) : base(fileOrServerOrConnection) { }
void InserttestTable(testTable instance)
{
using(var cmd = Connection.CreateCommand())
{
cmd.CommandText = "SELECT NEXT VALUE FOR [dbo].[seq_PK_testTable] as NextId";
cmd.Transaction = Transaction;
instance.id = (int)cmd.ExecuteScalar();
ExecuteDynamicInsert(instance);
}
}
}
[Table(Name = "dbo.testTable")]
public class testTable
{
[Column(DbType = "Int NOT NULL", IsPrimaryKey = true)]
public int id;
}
}
我认为它在内部仍然期望id 是0。如何强制 LINQ 反映真实身份?
PS:因为该问题已被标记为 LINQ to SQL insert primary key index 的重复项:IsPrimaryKey = true 和 IsDbGenerated = true 不起作用,因为它们导致 LINQ to SQL 生成查询 IDENTITY ID 的代码,即 @ 987654334@,如果 ID 是使用默认值 SEQUENCE 创建的,则返回 NULL。
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
-
@YvetteColomb 现在这个问题将永远不会被回答,而相同的 cmets 将被其他人发布
-
@PanagiotisKanavos 这里的每个人都有代表聊天。当 30 + cmets 太多时,会出现一个点
标签: c# .net sql-server linq-to-sql