identity-column

Linq to SQL - How to find the value of the IDENTITY column after InsertOnSubmit()

微笑、不失礼 提交于 2019-12-09 08:26:55
问题 I am using LINQ to SQL to insert simple data into a table WITHOUT a stored procedure . The table has a Primary Key ID column, which is set as an IDENTITY column in both SQL Server and in my DBML . First I call InsertOnSubmit(); with data for a single row, and then I call SubmitChanges(); to commit the row to the db. But I think there must be a simple way to then retrieve the row's IDENTITY column value of the newly inserted row. I don't wish to provide the IDENTITY column value to the db, I

Generate identity for an Oracle database through Entity Framework using an exisiting stored procedure

China☆狼群 提交于 2019-12-08 21:38:44
问题 How to automatically generate identity for an Oracle database through Entity Framework? I have a function that I could call and generate the column which is not in the context how do I explicitly call the stored procedure through Entity Framework? I am using a repository pattern. Random Number generator to insert a record (where I get the primary key through a UDF and pass this to the entity to insert). 回答1: 1) Create sequence in Oracle CREATE SEQUENCE dummy_test_seq MINVALUE 1 MAXVALUE

ExecuteStoreCommand returns -1 , EntityFramework , C#?

◇◆丶佛笑我妖孽 提交于 2019-12-08 03:17:00
问题 I wanna get the next automatically incrementing primary key, Here I understood to use T-SQL for doing that. So I wrote the following method : public int GetLastNewsID() { const string command = @"SELECT IDENT_CURRENT ('{0}') AS Current_Identity;"; int id = EntityModel.ExecuteStoreCommand(command, "News"); return id; } but it returns -1, whereas it must be 4 right now. P.S: When I execute below T-SQL in SQL Management Studio , it returns 4 USE T; GO SELECT IDENT_CURRENT ('NEWS') AS Current

T-SQL: CTE with identity columns

不羁岁月 提交于 2019-12-07 14:09:13
问题 I'm building a tree (bill of materials style), and transforming some data. Consider the following table: BillOfMaterials BomId ParentId Now I'm using a CTE to fill it up: with BOM as ( select @@identity as BomId, null as ParentId <some other fields> from MyTable union all select @@identity as BomId, parent.BomId as ParentId, some other fields from MyTable2 inner join BOM parent on blabla) insert into MyTable3 select * from BOM Problem is: the @@identity will only give me the identity of the

Can I make an identity field span multiple tables in SQL Server?

让人想犯罪 __ 提交于 2019-12-07 06:34:58
问题 Can I have an "identity" (unique, non-repeating) column span multiple tables? For example, let's say I have two tables: Books and Authors. Authors AuthorID AuthorName Books BookID BookTitle The BookID column and the AuthorID column are identity columns. I want the identity part to span both columns. So, if there is an AuthorID with a value of 123, then there cannot be a BookID with a value of 123. And vice versa. I hope that makes sense. Is this possible? Thanks. Why do I want to do this? I

SQL Reset Identity ID in already populated table

不打扰是莪最后的温柔 提交于 2019-12-07 02:54:18
问题 hey all. I have a table in my DB that has about a thousand records in it. I would like to reset the identity column so that all of the ID's are sequential again. I was looking at this but I'm ASSuming that it only works on an empty table Current Table ID | Name 1 Joe 2 Phil 5 Jan 88 Rob Desired Table ID | Name 1 Joe 2 Phil 3 Jan 4 Rob Thanks in advance 回答1: The easiest way would be to make a copy of the current table, fix up any parentid issues, drop it and then rename the new one. You could

ExecuteStoreCommand returns -1 , EntityFramework , C#?

好久不见. 提交于 2019-12-06 21:59:32
I wanna get the next automatically incrementing primary key, Here I understood to use T-SQL for doing that. So I wrote the following method : public int GetLastNewsID() { const string command = @"SELECT IDENT_CURRENT ('{0}') AS Current_Identity;"; int id = EntityModel.ExecuteStoreCommand(command, "News"); return id; } but it returns -1, whereas it must be 4 right now. P.S: When I execute below T-SQL in SQL Management Studio , it returns 4 USE T; GO SELECT IDENT_CURRENT ('NEWS') AS Current_Identity; GO You need to use ExecuteStoreQuery public int GetLastNewsID() { const string command = @

How to use %ROWTYPE when inserting into Oracle table with identity column?

我是研究僧i 提交于 2019-12-06 01:45:21
问题 I have an Oracle 12c database with a table containing an identity column: CREATE TABLE foo ( id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, bar NUMBER ) Now I want to insert into the table using PL/SQL. Since in practice the table has many columns, I use %ROWTYPE : DECLARE x foo%ROWTYPE; BEGIN x.bar := 3; INSERT INTO foo VALUES x; END; However, it give me this error: ORA-32795: cannot insert into a generated always identity column ORA-06512: at line 5 Since it is very good for code

Maintaining a foreign key relationship when inserting into tables with autoincrementing Id's

人走茶凉 提交于 2019-12-05 12:41:35
I have two tables: Defect and DefectData. Each Defect may or may not have one or many DefectData. As such DefectData has a DefectId column as a foreign-key. The Id in both tables is an autoincrementing identity. The problem I am having is that when I want to insert a new Defect and its DefectData the Defect is inserted first and gets an Id, but I don't know what that Id is to give to DefectData. My solution is to then select from defects matching inserted data to get the Id. Insert Defect Get that defect's Id Insert DefectData(zero or many) with Id from 2. Setting IdentityInsert on then

Can I make an identity field span multiple tables in SQL Server?

人走茶凉 提交于 2019-12-05 11:05:54
Can I have an "identity" (unique, non-repeating) column span multiple tables? For example, let's say I have two tables: Books and Authors. Authors AuthorID AuthorName Books BookID BookTitle The BookID column and the AuthorID column are identity columns. I want the identity part to span both columns. So, if there is an AuthorID with a value of 123, then there cannot be a BookID with a value of 123. And vice versa. I hope that makes sense. Is this possible? Thanks. Why do I want to do this? I am writing an APS.NET MVC app. I am creating a comment section. Authors can have comments. Books can