我有一个现有的数据库,我想用它来使用 EF4.0
构建一个新应用程序
某些表没有定义主键,因此当我创建新的实体数据模型时,我会收到以下消息:
The table/view TABLE_NAME 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.
如果我想使用它们并修改数据,我是否必须向这些表添加 PK,或者是否有解决方法,以便我不必这样做?
最佳答案
我认为 Tillito 解决了这个问题:
Entity Framework and SQL Server View
我将在下面引用他的条目:
我们遇到了同样的问题,这是解决方案:
要强制 Entity Framework 使用列作为主键,请使用 ISNULL。
要强制 Entity Framework 不使用列作为主键,请使用 NULLIF。
应用此功能的一个简单方法是将 View 的 select 语句包装在另一个 select 中。
示例:
SELECT
ISNULL(MyPrimaryID,-999) MyPrimaryID,
NULLIF(AnotherProperty,'') AnotherProperty
FROM ( ... ) AS temp
Tillito 于 2010 年 4 月 26 日 17:00 回答
关于c# - Entity Framework : table without primary key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3996782/