实体框架不工作的表没有标识列实体、标识、框架、工作

由网友(Forever、浅念ペ)分享简介:我有以下表格:create table tbl(id int identity(1,1),val varchar(100))现在当我使用实体框架对象映射到这个表,它的工作原理,但是当我更改表的定义如下:Now when i use Entity Framework to map objects to this...

我有以下表格:

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

现在当我使用实体框架对象映射到这个表,它的工作原理,但是当我更改表的定义如下:

Now when i use Entity Framework to map objects to this table, it works, however when i change the table definition as follows:

create table tbl1
(
    id int,
    val varchar(100)
)

实体框架不映射对象,此表。任何线索,为什么会出现这种情况是AP preciated。

Entity Framework does not maps objects to this table. Any clue as to why is this happening would be appreciated.

推荐答案

实体框架需要一个主键生成数据库模型。如果在表上没有主键便索性选择非空列作为一个连接主键和实体将被读取/只。

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.

在你的第一个表的身份定义,使你的id列非空的,所以你能够创建一个实体。你应该已经看到了这个消息,同时增加该表:

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:

表/视图'TBL1'没有定义一个主键的键   已推断和的定义创建为只读   表/视图。

"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."

在你的第二个表但没有非空列和EF不能为它创建一个实体。当您尝试添加它看到消息:

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:

表/视图'TBL1'没有定义主键,也没有   有效主键可以推断。此表/视图已   排除在外。要使用该实体,您需要查看您的架构中,添加   正确的钥匙,并取消它。

"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."

阅读全文

相关推荐

最新文章