Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq insert with no primary key

I need to insert records into a table that has no primary key using LINQ to SQL. The table is poorly designed; I have NO control over the table structure. The table is comprised of a few varchar fields, a text field, and a timestamp. It is used as an audit trail for other entities.

What is the best way to accomplish the inserts? Could I extend the Linq partial class for this table and add a "fake" key? I'm open to any hack, however kludgey.

like image 626
betitall Avatar asked Dec 02 '08 06:12

betitall


2 Answers

LINQ to SQL isn't meant for this task, so don't use it. Just warp the insert into a stored procedure and add the procedure to your data model. If you can't do that, write a normal function with a bit of in-line SQL.

like image 181
Jonathan Allen Avatar answered Sep 18 '22 05:09

Jonathan Allen


Open your DBML file in the designer, and give the mapping a key, whether your database has one or not. This will solve your problem. Just beware, however, that you can't count on the column being used for identity or anything else if there isn't a genuine key in the database.

like image 21
TheSmurf Avatar answered Sep 20 '22 05:09

TheSmurf