Can we have table without primary key in entity framework?

后端 未结 6 1935
北恋
北恋 2020-11-30 12:02

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?

6条回答
  •  时光说笑
    2020-11-30 12:39

    Starting with .NET Core 2.1, EF Core supports models without primary key.
    This is realized through a concept of Query Types instead of Enitity Types.
    See https://docs.microsoft.com/en-us/ef/core/modeling/query-types

    Some of the main usage scenarios for query types are:

    • Serving as the return type for ad hoc FromSql() queries.
    • Mapping to database views.
    • Mapping to tables that do not have a primary key defined.
    • Mapping to queries defined in the model.

    Query Types have limits though. They cannot be inserted, updated or deleted on the database. And they have only limited support of navigation properties.

提交回复
热议问题