identity-insert

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

我的梦境 提交于 2019-12-17 18:32:46
问题 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

IDENTITY INSERT and LINQ to SQL

£可爱£侵袭症+ 提交于 2019-12-17 11:01:18
问题 I have a SQL Server database. This database has a table called Item. Item has a property called "ID". ID is the primary key on my table. This primary key is an int with an increment value of 1. When I attempt to insert the record, I receive an error that says: Cannot insert explicit value for identity column in table 'Item' when IDENTITY_INSERT is set to OFF.". I am attempting to insert records using the following code: public int AddItem(Item i) { try { int id = 0; using (DatabaseContext

How to Insert Identity by AddOrUpdate methode in Seed

≡放荡痞女 提交于 2019-12-12 14:24:06
问题 I have an entity that has an identity column. As part of the data-seed I want to use specific identifier values for the "standard data" in my system. I dont want disable identity. only i want to set IDENTITY_INSERT ON in migration seed. My code is: protected override void Seed(DelphyWCFTestService.Model.DataContext context) { context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Cities] On "); context.Cities.AddOrUpdate( p => p.CityId, new City { CityId = 1, Title = "Paris" } ); }

I set IDENTITY_INSERT to ON but I get a SqlException saying it's OFF

时光怂恿深爱的人放手 提交于 2019-12-11 02:39:17
问题 I'm trying to grab thousands of invoices (and other stuff) from a text file and insert them into SQL Server 2008. I wrote a little console app to do this and it uses LINQ to SQL. After I insert all the existing invoices I want the Invoice_ID to be an identity column and auto-increment, so I have it designed as such: CREATE TABLE [dbo].[Invoice] ( [Invoice_ID] int IDENTITY(1,1) NOT NULL PRIMARY KEY, /* Other fields elided */ ) Before I start inserting invoices I call a stored procedure that

NHibernate - Insert with identity_insert ON

时光怂恿深爱的人放手 提交于 2019-12-08 03:30:30
问题 I have an issue where I am trying to re-insert an entity into my database. It is illustrated with the following unit test: // entity mapped to dbo.IdentityInsertTest table // dbo.IdentityInsertTest has an IDENTITY Primary Key, Id var id = (long)NHibernateSession1.Save(new IdentityInsertTest()); NHibernateSession1.Flush(); // delete previously created row ExecuteNonQuery("DELETE FROM dbo.IdentityInsertTest"); try { // set entity insert off so that I can re-insert NHibernateSession2

Sql Server Ce 3.5 Identity insert

情到浓时终转凉″ 提交于 2019-12-01 06:06:48
问题 got an issue with identity columns in Sql Server CE when using Server explorer, in VS2008, executing the following script SET IDENTITY_INSERT testTable ON; Insert into testTable (id,name) values (1,'Something') SET IDENTITY_INSERT testTable ON; sends the follow message error 'The Set SQL construct or statement is not supported.' but then inserts the row fine ?!?!?! anyway, when I try to do the same thing through C#, giving that script as a command text it fails saying the error was in the

Force SET IDENTITY_INSERT to take effect faster from MS Access

跟風遠走 提交于 2019-11-29 15:52:30
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.Execute "INSERT INTO temp_To SELECT * FROM temp_From", dbFailOnError ExecutePassThru "SET IDENTITY_INSERT "

How to automatically reseed after using identity_insert?

梦想的初衷 提交于 2019-11-29 03:12:49
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) ? Tommy Use the information in this link in combination with a SQL function that gets the max(RID) from each table that you need to reset. For instance, if you

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

房东的猫 提交于 2019-11-29 00:48:38
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! Eon Since SET IDENTITY_INSERT is a session sensitive, it is managed in buffer level without storing somewhere. This means we do not need to check the IDENTITY_INSERT status as we never use this key word in current session.

Using IDENTITY_INSERT with EF4

倖福魔咒の 提交于 2019-11-28 09:58:06
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. Devart 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 set to this column in DB. So, if you have an auto-incremented column in your database and the ADO.NET