Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

c# - Can we have table without primary key in entity framework?

I was just practicing code first new database entity framework from msdn, I wanna know whether a table without primary key can be created in code first new database EF?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...