实体框架不工作的表没有标识列 [英] entity framework not working on table without identity column

查看:275
本文介绍了实体框架不工作的表没有标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格:

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

这篇关于实体框架不工作的表没有标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆