Primary key auto increment - c#

Problems: Previously i called the function below.
ArchiveDeletedPatient.ID = 18
_archiveDB2.patient.Add(ArchiveDeletedPatient);
_archiveDB2.SaveChanges();
Afterward I deleted all records from patient database(delete from patient), now in my database it is empty. I called the above function again, however this time it auto increment ID to 19 and inserted into the database. I spamed the above functions and it kept incrementing. How to remove such function?? I don't need the auto increment.
Some might say it is because of database setting. I tried manually insert using Microsoft SQL Server Management Studio 17 "INSERT INTO PATIENT TABLE VALUES(18, ...). It allowed me to insert and when there is such ID in the table. It will not auto increment and will just prompt me a error. How can i remove such auto incrementing feature in c# linq.

Auto increment feature is not of linq c#.
See the table properties and can you edit the pk to remove "identity" for remove autoincrement o add "identity".
If identity is activated in bbdd, the id is calculated in database and linq return this into your entity.

The auto incrementing is not the feature of Linq. It's the feature of Database.
Here are complete instructions to remove auto increment from MS SQLServer:
Open your Database.
Right click on your Table and select Design.
Select your specific column on which you want to remove auto incrementing.
In the column properties window click on Identity specification field and make this unchecked.
Save table design.
Here it is...
Note:
If you're using Entity Framework and specifying your field with [DatabaseGenerated(DatabaseGeneratedOption.Identity)] remove this annotation
or
replace it with [DatabaseGenerated(DatabaseGeneratedOption.None)].

Related

Insert to Oracle Database always creates new Index ID and doesn't take the old one

I am currently trying to insert a DataRow into an Oracle SQL Database. The entry gets inserted into the Database but always gets a new ID. I am providing an ID in the Insert Command but it always uses an auto increment and doesn't let me insert my ID.
What can I do?
There are 2 possible solutions to your problem.
Set column as NOT to be "Identity" column. This will take care of
your issue without any hassle.
If you must keep that column as "Identity" column, set "Generated"
property to "By Default on Null". This will ensure that when you are
NOT passing any value for ID, the system generates next sequence
number automatically.
For solution 2:
DDL Statement: ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY INCREMENT BY 1
Table Creation in SQL Developer:

How do you setup table structure for auto increment primary key with revision?

C# interface accessing Sql server db.
I have a table with a composite key ID and revision.
When I "insert" a new record I want to auto increment the ID.
When I "update" the record I want to actually insert a new record but increment revision.
What is the proper table settings for this?
What I have done in the past is actually use a trigger for UPDATE. It would copy all the values into a new record and increment the version column. I would also have an indexed column that would serve as a key, but not a primary key so that all revisions would be tied together.

Query on Identity Column on MS SQL DB

I have created table in MS SQL 2008 with one identity column(Start Value - 1 and Increment is also 1) and 4 other columns. I am accessing this DB from C# ASP.NET. Used to push data only for the non identity column. Identity column will auto increment itself.
As of now i am manually querying the column value with the remaining for columns. But I am facing problem if all the other four column values are equal i am not getting the exact value which i am looking for
Now my query is, Is there any why in C# where I can get the value of the newly created identity column whenever new record is created.
Thanks.
You can use
SCOPE_IDENTITY()
Which will returns the primary key value of the recently inserted row
The answer to your question actually lies in SQL Server. You can run:
SELECT ##identity
after your insert to get the last inserted row identity.
http://technet.microsoft.com/en-us/library/aa933167(v=sql.80).aspx
EDIT BASED ON COMMENTS:
Consider using SCOPE_IDENTITY() as referenced here:
http://technet.microsoft.com/en-us/library/aa259185(v=sql.80).aspx
In SQL terms you can output the records back if you wish it. But how you might apply this to C# is up to you. Example:
INSERT INTO TABLE_A (SOMETHING, SOMETHINGELSE, RANDOMVAL3)
OUTPUT inserted.A_ID, inserted.SOMETHING, inserted.SOMETHINGELSE, inserted.RANDOMVAL3
SELECT 'ASD','DOSD', 123
But unless you're using merge, you can't use OUTPUT to print out any values from joining tables from an INSERT. But that's another matter entirely, I think.
Also, it's hardly good practice to bounce this data between the application and the DB all the time, so I'd look to alternatives if possible.

How Can I Programmatically Override the Auto_Increment Value When Inserting Into a DB

I have just started converting an old web application into a new structure.
I have a newly designed supplier table created in a MS SQL Server 2012 DB, which I am in the process of transferring the supplier data from the old DB to.
I have a C# MVC application set-up using the new DB as the data context. At present the application gets the data from the old applications DB, checks it, reformats it where necessary and inserts it into the new database in the desired format.
However as users of the application have got used to the supplier IDs in the old DB, I don't want to change the IDs, however as the id field in the new supplier table is an auto increment, it doesn't copy the ids from the old database. It just simply increments.
Does anybody know of anyway the auto increment can be overridden programmatically in the c# code for the initial import stage of the project. Once the data from the old system has been imported, I would then like to revert back to auto increment.
You can set identity insert on. However, if the users know the keys I've found they start asking to change them. I might consider the old keys as the user identifier and create a new surrogate key they won't ever see.
You can turn off the identity generation on the server temporarily during the bunch of inserts, then turn it on again. Try submitting this query to the server:
SET IDENTITY_INSERT dbo.sometable ON ;
INSERT INTO dbo.sometable (id,.......) VALUES (456,.......) ;
INSERT INTO dbo.sometable (id,.......) VALUES (276,.......) ;
INSERT INTO dbo.sometable (id,.......) VALUES (387,.......) ;
SET IDENTITY_INSERT dbo.sometable OFF ;
It just disables identity creation, then does the inserts (note that the column and value lists do include the id), then restores the default behavior.
In Sql Server, you can set the ID field to non identity. This will make it not auto increment

Inserting data into C# Datasets

I am using C#.Net application in which i created a dataset.Now i want to create a method in which i will enter a record in one table which will return a value ie primary key.Now by using that primary key i have to insert records in 5 tables and i have to use that primary key as a foreign key for this 5 tables using dataset.
You need to tell your parent table's table-adapter to refresh the
data-table after update operation.
This is how you can do that.
Open the properties of ParentTableAdapter -> Default Select Query -> Advnaced options. and Check the option of Refresh the data table. Save the adapter now. Now when you call update on table-adapter, the data-table will be updated [refreshed] after the update operation and will reflect the latest values from database table. if the primary-key or any coloumn is set to auto-increment, the data-table will have those latest value post recent update.
Now you can Call the update as ParentTableAdapterObj.Update(ds.dataTable);
Read latest values from the ParentDataTable(ds.dataTable) coloumns and assign respective values into the child table before update. This will work exactly the way you want.
alt text http://ruchitsurati.net/files/tds1.png
Have you tried temporary key linkage until the DB generates the final key? Works great for me.

Categories

Resources