c# - Entity Framework : table without primary key

标签 c# .net entity-framework entity-framework-4

我有一个现有的数据库,我想用它来使用 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/

相关文章:

c# - 如何通过互联网推送数据?

c# - 在响应中出现错误

c# - Entity Framework 6 Code First - 对树结构的评论

c# - 从数据库中检索属性的简单类型的创建模式?

c# - 工具箱中没有报告项 (VS 2008 SP1)

c# - UWP 应用程序和 WPF 应用程序通过本地网络通信

c# - MySQL Entity Framework - 当多个元素作为参数传递时,Contains 方法会引发异常

c# - Entity Framework 6 无法检索元数据

c# - 如何输出以控制 bool 值?

c# - 将 datarow[] 转换为 List<int> 的最佳方法是什么?