开发者

how to insert data in sql table using Linq without seting any primary key in table

I have a Table in which i don't want to specify any primary key, after that i am inserting records in it using Linq...aahhh...its giving the error "Can't开发者_运维问答 perform Create, Update or Delete operations on 'Table(abc)' because it has no primary key"

can ani one tell me how to insert record without setting it primary key. By the way im not setting any primary key because this table will have bulk of data to keep.


You can't use Linq-to-SQL directly with tables that don't have primary keys, because it's not supported.

If you're worried about the performance hit of indexing, what you can do is add a Stored Procedure that does the insert and add that to your data context. It's a bit more work, and it's not really Linq-to-SQL, it'll just be a method on that you call on your data context.

There probably won't be a noticeable performance hit on an identity primary key field anyway.


Tami - would it not be a good idea to stick to 'best practices' when using linq and add a autonumber primary key, even if it isn't going to be used other than for inserts or updates?? i can think of many instances where the seemingly 'non requirement' for a primary key later leads to trouble when trying to update to other platforms etc.

If there's a compelling reason to not add a 'blind' primary key, then it might help to detail this as well in the question. I can't think of any reasons not to add it, especially if it means that you don't have to code around the limitation.

jim

[edit] Tami - i'll be honest with you. you might have to investigate conventions to best satisfy any answer to this question. basically, altho' you don't 'need' an index on your records, due to not being edited or deleted, the convention with linq is based around the assumption of data integrity. in essence, linq (and many other programatic tools) require a convention that allows them to succinctly identify a unique key on each object that they bring into scope. by not defining this, you are by-passing this convention and therefore linq is flagging this up for you. the only way fwd is to go with the flow. even if you 'feel' that the index is redundant, linq requires it to allow you to access the full functionality built into it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜