how i can add the rows in table when the table not have primary key.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Let me illustrate with a sample:
The above code was giving the same error when trying to insert a new row. The reason behind this issue is that LINQ does not provide the facility to insert data into a table without a primary key. At this point you have two options which you can use to perform the insert operation.
1.Create a store procedure and call it from LINQ.
Here I have created a stored procedure with the name
InsertEmployeeData
, and called it from the code. Simple and straight forward.2.Create an insert statement and execute using LINQ.
Here I created a normal insert query and executed using the the LINQ
ExecuteQuery
method.Some tables (like Vendor provided tables) DO NOT HAVE A PRIMARY KEY!
Every answer says "add a primary key".
Look, I wouldn't be asking if I could just add one. The conditions are preset not by me.
Is there a way to JUST WRITE. No reading, deleting or updating is required.
If you can't set it in the db, set the primary key in the linq2sql designer.
If there is no subset of columns that can be used as primary key, mark them all as part of the primary key.
I may be wrong here, since I have no way to verify right now, but I believe you can tell Linq to Sql that a field is a primary key, even if it's not actually one in the database. This assumes that the data in the field is in fact unique and can be used as a key even if it's not defined as one.
If the table has no primary key, but contains unique data that can be used as a key, then tell L2S in the dbml that this is the primary key.
If the table does not contain unique data that can be used as a key, then you have to write a sproc that deletes the data you want to delete.
As your question's title says, LINQ to SQL can not perform create, update or delete operations on a table without a primary key. It's not possible.
So, you'll need to perhaps use DataContext.ExecuteCommand() to do those things, or better yet, refactor your database so the tables have primary keys.
i dont know if this is obvious but maybe you could set a primary key. http://www.functionx.com/vb/adonet/primarykey.htm
also see http://www.i-think22.net/archives/2009/07/24/linq-to-sql-and-tables-with-no-primary-key/