change primary key with linq in c# - c#

How can I change primary key with linq in c#
I try
db.SubmitChange();
an error occur.
Then I realized linq does not allow to change primary key field in my c# program
How can I fix this problem.

You can't update a primary key using LINQ. You need to delete that row from the database and add a new one with the new primary key. You can do that in the database using SQL but it should be Unique and shouldn't violate any foreign key constraint.

Related

C# sql copy rows with 3 primary keys

I am working on an existing table where as I can see there are 3(!) primary keys:
I want to copy the existing rows, alter the ctid column and then copy them again to the end of the table. I try that and I am getting the error:
Cannot add an entity with a key that is already in use.
Probably because I am copying the rows and adding them with the same primary keys. How I can solve this? Is it possible to solve it without modifying the db schema (I am thinking of adding ctid as primary key also)?
Code
var testsDefault = (from i in dc.TestUnits
where i.ctid == null
select i).ToList();
List<DAL.TestUnit> TestList = new List<DAL.TestUnit>();
foreach (var test in testsDefault)
{
DAL.TestUnit newTest = new DAL.TestUnit();
newTest.TestID = test.TestID;
newTest.PatientType = test.PatientType;
newTest.Unit = test.Unit;
newTest.ctid = "105";
TestList.Add(newTest);
}
dc.TestUnits.InsertAllOnSubmit(TestList);
dc.SubmitChanges();
You need to add ctid to your composite primary key.
ALTER TABLE TestUnits
DROP CONSTRAINT PK_WhateverYourCompositeIndexNameIs
ALTER TABLE TestUnits
ADD CONSTRAINT PK_WhateverYourCompositeIndexNameIs PRIMARY KEY (TestID, PatientType, Unit, ctid)
See: How can I alter a primary key constraint using SQL syntax?
No,it is not possible what you are trying to do without modifying the db schema. Since you are using three PK and Primary key can not be duplicate as you are trying to do. The solution to your problem is make all the row's columns unique and then add the another row but make sure all tuples must have unique entries.
The another solution to your problem is already given by Rafalon

How to allow save duplicate keys in sql database

I am working with SQL Server 2008. I want to add duplicate keys on database. Presently it shows error while inserting duplicate key.
How can I insert duplicate keys into database?
My insertion query is
string qry = "insert into EmpMaster values('"id+"','"+code+"','"+type+"','"+fname+"')";
SqlDataReader dr1 = conn.query(qry);.
Primary key is code.
You can't insert duplicate to primary key column. Primary key is for not null and non-duplicate values to do so remove primary key column name from code. or take another column which can insert duplicate
Suggestion :- Removing primary key is not a good practice. If you want to add duplicate keys then you can use another column
Thanks.
You can't add duplicate to a primary key column.
If you want to add duplicate data in COLUMN "CODE", just normalize your database and make foreign key references .

Updating relations in Linq To SQL is possible?

I need some functionality in my project and I don't know if its possible.
Here is a pic with the relations:
I need to update the keys relation table LessonByFacultyMember and the same keys in Scheduling table.
I mean the keys LessonNumber,LessonCoursenumber,FacultyMemberId (LessonByFacultyMember table)
and LessonNumber,CourseNumber,FacultyMemberId (Scheduling Table).
Is it possible to update this kind of relations?
UPDATE:
I just want to be clear that i mean the possibility to change the VALUE that stored in the keys dynamically in some method.
Yes you can do so by selecting Update Cascade option in Foreign key in the database.This options automatically updates the key values in the other tables. But in your case this is not needed. The table LessonByFacultyMember should have a column LessonByFacultyMemberId as a primary key and that should be in the Scheduling table as a Reference instead of putting all the three columns in the Scheduling table. If you do so ,you don't need to worry about the updating LessonNumber,CourseNumber,FacultyMemberId in the Scheduling Table. Also in your Scheduling table there should be a column SchedulingID as a Primary Key. You can take LessonByFacultyMemberId ,SchedulingID as an auto incremented integer. Also there is no need to make LessonNumber,CourseNumber,FacultyMemberId as a Primary key in the LessonByFacultyMember table. Instead you need to make them as unique key. Similarly in Scheduling table make the current primary key as unique key and have SchedulingId as primary key. In case of showing records you need to make select statement using joins and it is better to create a view for such statement. In case if still it is not clear , create a sqlFiddle on http://sqlfiddle.com/ for your schema and share that in your question or comment to this answer. I will update the same.

Data Import Export Issue C#/SQL Server

I need to implement an import export module for three database tables which are internally related with primary key/foreign key constraints.
I can do both part easily using c#/linq to xml/linq to sql. Problem is, when i am importing the exported data, in database, primary key tables' id are regenerating without taking/assign the id from xml file(its natural for auto increment). For this, other tables data can't be imported/showing error that primary key constraint violates.
My goal is to import all data and keep the primary/foreign key intact.
Now, can anyone please suggest me what I can do to solve this issue please? Thanks in advance.
You can use identity insert to specify a value for identity columns.
If you specify explicit values for identity columns, you yourself are responsible for resolving conflicts. If the database already has a row with id = 1, you have to decide what to do if the import also contains id = 1: SQL Server will just throw an error.
We always used BCP operations for such tasks.
I would suggest to not make your autoincrement the primary key but to have a separate primary key which can be defined somehow else... then you won't have the problem with the autoincrement
If you don't need the ids in the XML and SQL to match (only the relationships) you could use SCOPE_IDENTITY() and maintain mapping from XML ids to SQL ids to insert properly foreign key columns in other tables.

LINQ- inserting into conjoining tables

I've got a conjunction table, which is a one to many relationship. My LINQ code will not insert into this table because it has no primary key, even though the table is composed of just foreign keys. In addition, when I try to save the relationship for one of my foreign keys, it says that the ALTER_TABLE statement conflicts with the FOREIGN_KEY constraint. But I've checked all through both of the tables in question and there is no ALTER_TABLE anywhere.
How can I make changes that will allow me to insert into my conjunction table?
Try creating a primary key on the table, that is a composite key of the foreign key columns. LINQ-to-SQL should like that a lot better. It will allow you to keep your current structure but provide a key that LINQ-to-SQL can use.

Categories

Resources