Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 我是否需要在源表上有一个PK来创建ADO.NET实体数据模型?_Sql Server_Asp.net Mvc_Visual Studio 2008_Entity Framework_Primary Key - Fatal编程技术网

Sql server 我是否需要在源表上有一个PK来创建ADO.NET实体数据模型?

Sql server 我是否需要在源表上有一个PK来创建ADO.NET实体数据模型?,sql-server,asp.net-mvc,visual-studio-2008,entity-framework,primary-key,Sql Server,Asp.net Mvc,Visual Studio 2008,Entity Framework,Primary Key,为了在MVC应用程序中使用它,我正在尝试“实体化”一些外部表(我不管理这些表),原则上,我的尝试(VS2008输出)并不是非常成功: 错误列表[0个错误][0个警告][1条消息] 描述 The table/view 'DATABASE.dbo.table' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To us

为了在MVC应用程序中使用它,我正在尝试“实体化”一些外部表(我不管理这些表),原则上,我的尝试(VS2008输出)并不是非常成功:

错误列表[0个错误][0个警告][1条消息]

描述

The table/view 'DATABASE.dbo.table' 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.
文件

线

纵队

1
计划

MVC_Entity_Test
输出

显示来自:实体数据模型的输出

Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
The model was generated with warnings or errors.
Please see the Error List for more details. These issues must be fixed before
running your application.
Loading metadata from database took 00:00:06.2809306.
Generating model took 00:00:03.0359078.
Writing out the EDMX file took 00:00:00.0230083.
Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
The model was generated with warnings or errors.
Please see the Error List for more details. These issues must be fixed before running
your application.
Loading metadata from database took 00:00:12.3208290.
Generating model took 00:00:03.6914563.
Writing out the EDMX file took 00:00:02.1670689.
所以我的问题是,在源表上声明PK以将其正确映射为ASP.NET实体是绝对必需的吗


注意:我不希望这篇文章成为一篇关于伟大的、标准驱动的、学术支持的数据库建模的论文,问题是,我必须处理来自某个视图的外部表,来自某个表的外部表,诸如此类,所以我真的无法控制它。关键是我只想完成我这方面的工作。

好吧,我通常建议将主键放在任何表上作为一种最佳做法,除非在非常罕见的情况下,如用于批量加载数据的表等

毕竟,只有使用主键,您才能唯一地标识和分隔表中的行(以及域模型中的实体或对象实例)

一位SQL专家甚至说:如果它没有主键,它就不是表!:-)


Marc

好吧,我通常会推荐在任何表上放置主键作为最佳实践,除非在极少数情况下,如用于批量加载数据的表等

毕竟,只有使用主键,您才能唯一地标识和分隔表中的行(以及域模型中的实体或对象实例)

一位SQL专家甚至说:如果它没有主键,它就不是表!:-)


Marc

实体框架是一个映射框架,因此它需要一种方法将每一行唯一地映射到一个对象。这需要某种形式的唯一标识符,用于生成SQL DML语句,以便修改行


在您的情况下,如果列的组合不包含唯一标识行的空值,则可以手动将必要的信息添加到edmx文件中。此MSDN页面将介绍手动将对象添加到edmx的基础知识。请记住,它只是一个可以编辑的XML文件。

实体框架是一个映射框架,因此它需要一种方法来唯一地将每一行映射到一个对象。这需要某种形式的唯一标识符,用于生成SQL DML语句,以便修改行


在您的情况下,如果列的组合不包含唯一标识行的空值,则可以手动将必要的信息添加到edmx文件中。此MSDN页面将介绍手动将对象添加到edmx的基础知识。请记住,它只是一个可以编辑的XML文件。

Marc,谢谢你的评论,但正如我在注释中解释的,我对该数据源没有任何级别的控制,但我仍然希望依靠实体来操作它。Marc,感谢您的评论,但正如我在注释中解释的,我对该数据源没有任何级别的控制,但我仍然希望依靠实体来操作它。兰斯,非常感谢您的帖子!我一直在检查文档,我必须说,现在我对EF更有信心,我将尝试手动编辑这些XML。手动操作时,请查看工具edmgen.exe。我只使用了一点点,但是它可以用来验证XML文件,这可以帮你省去一些麻烦。兰斯,非常感谢你的帖子!我一直在检查文档,我必须说,现在我对EF更有信心,我将尝试手动编辑这些XML。手动操作时,请查看工具edmgen.exe。我只使用了一点点,但是它可以用来验证XML文件,这可以帮你省去一些麻烦。
MVC_Entity_Test
Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
The model was generated with warnings or errors.
Please see the Error List for more details. These issues must be fixed before
running your application.
Loading metadata from database took 00:00:06.2809306.
Generating model took 00:00:03.0359078.
Writing out the EDMX file took 00:00:00.0230083.
Added the connection string to Web.Config.
Successfully registered the assembly 'System.Data.Entity, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' in Web.Config.
The model was generated with warnings or errors.
Please see the Error List for more details. These issues must be fixed before running
your application.
Loading metadata from database took 00:00:12.3208290.
Generating model took 00:00:03.6914563.
Writing out the EDMX file took 00:00:02.1670689.