entity framework not working on table without identity column

前端 未结 2 945
太阳男子
太阳男子 2020-12-19 07:14

I have the following table:

create table tbl
(
    id int identity(1,1),
    val varchar(100)
)

Now when i use Entity Framework to map obje

2条回答
  •  被撕碎了的回忆
    2020-12-19 08:07

    Entity Framework requires a Primary Key to generate a model from the database. If there is no Primary Key on a table it will simply select the non-nullable columns as a concatenated primary key and the Entity will be read/only.

    In your first table identity definition makes your id column non-nullable so you were able to create an Entity. You should have seen this message while adding that table:

    "The table/view 'tbl1' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view."

    In your second table however there is no non-nullable column and EF cannot create an Entity for it. See the message when you try to add it:

    "The table/view 'tbl1' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it."

提交回复
热议问题