从动态数据 Web 应用程序的“添加条目”页面中删除自动生成 GUID 列

发布于 2024-07-13 04:23:51 字数 167 浏览 11 评论 0原文

我有一个动态数据实体 Web 应用程序,它使用带有 GUID 的数据库作为表的主键。 所有 GUID 字段的默认值都设置为 NEWID(),因此 SQL Server 将为任何记录生成新的 GUID。 然而,在我的动态数据网站中,在插入新条目页面中,GUID 字段显示,并且预计用户输入数据。 如何防止该字段显示?

I have a Dynamic Data Entities Web Application that uses a database with GUIDs as primary keys for tables. All of the GUID fields have the default value set to NEWID() so SQL Server will generate a new GUID for any record. However in my dynamic data web site, in the insert new entry pages the GUID field shows up and it is expected for the user to enter data. How could I prevent the field from being displayed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒浓于脸红 2024-07-20 04:23:51

根据 MSDN

“您可以像这样隐藏主键通过使用 Scaffold 属性并将其值设置为 false 的任何其他列,由于无法识别自动生成的键,因此默认插入页面模板不适用于具有自动主键的表的 EDM,除非您为实体创建分部类并应用值为 false 的 Scaffold 属性。”

另外,如果您使用 SQL Server 2005 或更高版本,最好使用“newsequentialid()”作为 Guid 的默认值,因为它的插入效率更高。

According to MSDN:

"You can hide the primary key as you do any other column by using the Scaffold attribute and setting its value to false. Because automatically generated keys are not recognized, the default Insert page templates do not work with EDM for tables that have automatic primary keys unless you create a partial class for the entity and apply the Scaffold attribute with a value of false."

Also, as an aside it's preferable to use "newsequentialid()" for the default value of Guid if you're using SQL Server 2005 or higher as it's more efficient for inserting.

谜兔 2024-07-20 04:23:51

不是正确的答案..

完成此操作后(我就是这样做的),您可以插入 1 条记录,该记录将获得全零的 GUID。 之后,您将不会收到任何错误消息,但您将无法插入更多记录,因为 guid 上的唯一索引约束(当然,它是主键)。

要真正解决此问题,您必须编辑 emdx 文件并将其添加

StoreGeneratedPattern="Identity"

到“SSDL 内容”注释下的 Guid 属性标记中。 示例:

   <Property Name="Guid" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Identity"/>

如果您在 Visual Studio 中的设计器中执行此操作,它将在“CSDL 内容”注释下注释属性标记,但这不起作用。

使用 Entity Framework 4.0 Visual Studio 10 和 .NET Framework 4.0

Not the right Answer..

After doing this, which I did, you can insert 1 record which will get a GUID with all zero's. After that you will get no error message at all, but you will be unable to insert any more records, because of the unique index constraint on the guid ( which is a primary key of course ).

To really solve this problem, you have to edit the emdx file and add the

StoreGeneratedPattern="Identity"

into your Guid property tag under the "SSDL content" comment. Example:

   <Property Name="Guid" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Identity"/>

If yhou do this in the designer within visual studio it will annotate the property tag under the "CSDL content" comment which doesn't work..

Used Entity Framework 4.0 Visual studio 10 and .NET framework 4.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文