identity-insert

Why does this EF insert with IDENTITY_INSERT not work?

ぃ、小莉子 提交于 2020-03-18 03:57:48
问题 This is the query: using (var db = new AppDbContext()) { var item = new IdentityItem {Id = 418, Name = "Abrahadabra" }; db.IdentityItems.Add(item); db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Test.Items ON;"); db.SaveChanges(); } When executed, the Id of the inserted record, on a new table, is still 1. NEW: When I use either the transaction, or TGlatzer's answer, I get the exception: Explicit value must be specified for identity column in table 'Items' either when IDENTITY_INSERT is

How to switch between DatabaseGeneratedOption.Identity, Computed and None at runtime without having to generate empty DbMigrations

自闭症网瘾萝莉.ら 提交于 2019-12-30 04:40:12
问题 I am migrating a legacy database to a new database which we need to access and "manage" (as oxymoronic as it might sound) primarily through Entity Framework Code-First. We are using MS SQL Server 2014. The legacy database contained some tables with computed columns. Typical GUID and DateTime stuff. Technically speaking, these columns did not have a computed column specification, but rather where given a default value with NEWID() and GETDATE() We all know that it is very easy to configure the

H2 equivalent to SET IDENTITY_INSERT

若如初见. 提交于 2019-12-25 02:44:55
问题 I'm using H2 to JUnit test a system that will run using MS SQL Server in production. As part of the latest version I need to make some DDL changes and migrate data from the old structure to the new. And that will involve copying data into a new table that has an identity column. I'm actually splitting a single table into two. Going forward the new table will have an independent (identity) primary key, but for initialization it needs to have the same value for the primary key as the original

proper way of updating sql server table using access front end

爱⌒轻易说出口 提交于 2019-12-23 05:37:16
问题 i have a front end in access and back end is sql server 2008 one of the fields is the account number and here are the rules it is a zipcode like 92111 plus a dash plus a number. so the first one would be 92111-1, the second 92111-2 this has to do with how many clients we have in the zip code i would like this zip code to be automatically generated. here is what i need: the user enters the zip code i have a stored procedure that checks if this zip code exists already, to increment it: if 92111

Force SET IDENTITY_INSERT to take effect faster from MS Access

巧了我就是萌 提交于 2019-12-18 09:08:07
问题 I'm working on upsizing a suite of MS Access backend databases to SQL Server. I've scripted the SQL to create the table schemas in SQL Server. Now I am trying to populate the tables. Most of the tables have autonumber primary keys. Here's my general approach: For each TblName in LinkedTableNames 'Create linked table "temp_From" that links to the existing mdb' 'Create linked table "temp_To" that links to the new SQL server table ExecutePassThru "SET IDENTITY_INSERT " & TblName & " ON" db

How to put SET IDENTITY_INSERT dbo.myTable ON statement

Deadly 提交于 2019-12-18 04:40:29
问题 What I need to do is have a SET IDENTITY_INSERT dbo.myTable ON statement, what's the syntax of using the above statement in a c# app? 回答1: It's just the same as any other bit of SQL: using (var connection = new SqlConnection("Connection String here")) { connection.Open(); var query = "SET IDENTITY_INSERT dbo.MyTable ON; INSERT INTO dbo.MyTable (IdentityColumn) VALUES (@identityColumnValue); SET IDENTITY_INSERT dbo.MyTable OFF;"; using (var command = new SqlCommand(query, connection) { command

Linq to SQL: Why am I getting IDENTITY_INSERT errors?

折月煮酒 提交于 2019-12-18 04:20:52
问题 I'm using Linq to SQL. I have a DataContext against which I am .SubmitChanges()'ing. There is an error inserting the identity field: Cannot insert explicit value for identity column in table 'Rigs' when IDENTITY_INSERT is set to OFF. The only identity field is "ID", which has a value of 0. It's defined in the DBML as: [Column(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] There are a few foreign keys, and I've verified they

How to automatically reseed after using identity_insert?

白昼怎懂夜的黑 提交于 2019-12-18 03:55:01
问题 I recently migrated from a PostgreSQL database to a SQL Server database. To switch the data over I had to enable IDENTITY_INSERT. Well come to find out that I get all sorts of strange errors due to duplicate identity values(which are set as primary keys) upon doing an insert in any of the tables. I have quite a few tables. What would be the easiest way of automatically reseeding the identity of every table so that it is after max(RID) ? 回答1: Use the information in this link in combination

Using IDENTITY_INSERT with EF4

拜拜、爱过 提交于 2019-12-17 19:27:27
问题 I am trying to create a record in the db that has a predefined primary key value. I know how to do this with sql, but I was wondering if EF can do this for me? Otherwise, I will have to create a stored proc for the inserts. 回答1: In case you have the StoreGeneratedPattern attribute set to "None" for your entity, the Entity Key value you specify for it will be passed to database. In case this attribute is set either to "Identity" or to "Computed" there is no way to control what value will be

How do you check if IDENTITY_INSERT is set to ON or OFF in SQL Server?

爷,独闯天下 提交于 2019-12-17 18:33:55
问题 I've searched for this, but threads in which it appeared tended to have answers from people who didn't understand the question. Take the following syntax: SET IDENTITY_INSERT Table1 ON How do you do something more like this: GET IDENTITY_INSERT Table1 I don't want to do anything whatsoever to the data in the database or to the settings to get this information though. Thanks! 回答1: Since SET IDENTITY_INSERT is a session sensitive, it is managed in buffer level without storing somewhere. This