Can we have table without primary key in entity framework?
- Entity Framework must have a key identified on the entity (POCO class) that models the table.
- The key you define in Entity Framework does NOT need to be present in the underlying database (e.g. sql table).
If your SQL table does not have a primary key, you can still model it in Entity Framework, you will just need to define a key for that Entity. Pick one or more (possibly all) columns on the Entity that, when combined, will uniquely identify that instance within a collection of the entities. Note this is only important for updating or deleting entities, in that they need to be uniquely identified against the others in that collection to target the change. Find/Select and Add/Insert do not require this consistency.
There is a great difference between what EF can do with a database, and what is possible with a database.
Most databases allow for a table to be without a primary key. Most databases also allow for a table to be without a clustered index / Index Organized Table (or what ever it is the specific term for it in other database systems).
There is nothing wrong with that, and one should not state that it is a bad idea to have a table without a PK.
As always, it dependes on the needs and usage of the specific table. e.g. a log table, does not need a PK. It will never be used as a FK, so what is it use?
Bottom line, EF does not support tables without a key out of the box, there are some weird workarounds, but none that I have seen is good enough. That is a shame.
No you can't because Entity Framework needs to know the key to keep track on the object when you make an update or delete operation.
Anyway it's not a good idea to have a table without a PrimaryKey