简体   繁体   English

ASP.NET vNext DataAnnotations DatabaseGenerated无法正常工作

[英]ASP.NET vNext DataAnnotations DatabaseGenerated not working

I'm trying define a model ID variable like this in ASP.NET 5: 我正在尝试在ASP.NET 5中定义一个模型ID变量:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

To support data annotations I've added the package System.ComponentModel.DataAnnotations in my project.json file like this: 为了支持数据注释,我在project.json文件中添加了System.ComponentModel.DataAnnotations包,如下所示:

"System.ComponentModel.Annotations": "4.0.10-beta-22811"

And in the model cs file I've added using System.ComponentModel.DataAnnotations.Schema; 在模型cs文件中,我using System.ComponentModel.DataAnnotations.Schema;添加了此文件using System.ComponentModel.DataAnnotations.Schema;

Though I get the following error: 虽然我收到以下错误:

Error CS0433 The type 'DatabaseGeneratedAttribute' exists in both 'System.ComponentModel.Annotations, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 错误CS0433类型'DatabaseGeneratedAttribute'在'System.ComponentModel.Annotations,Version = 4.0.10.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'和'System.ComponentModel.DataAnnotations,Version = 4.0.0.0,Culture = neutral,PublicKeyToken中都存在= 31bf3856ad364e35'

And I don't know how I should fix this really. 而且我不知道该如何解决。 I've tried to include the namespace System.ComponentModel.Annotations instead of System.ComponentModel.DataAnnotations but it seems like it doesn't exist as I get this error then: 我试图包括名称空间System.ComponentModel.Annotations而不是System.ComponentModel.DataAnnotations但由于我收到此错误,所以它似乎不存在:

Error CS0234 The type or namespace name 'Annotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?) 错误CS0234类型或名称空间名称“ Annotations”在名称空间“ System.ComponentModel”中不存在(您是否缺少程序集引用?)

And if that namespace does not exist I don't understand how I can get the previous error which tells me that DatabaseGeneratedAttribute exists in two places. 而且,如果该命名空间不存在,我将无法理解如何获得先前的错误,该错误告诉我DatabaseGeneratedAttribute存在于两个地方。

I would really appreciate all help I can get with this. 我真的很感谢我能为此提供的所有帮助。

You can just use the KeyAttribute. 您可以只使用KeyAttribute。 That should do the auto generation for you. 那应该为您自动生成。

[Key]
public int Id { get; set; }

This attribute is available in the System.ComponentModel.DataAnnotations namespace 该属性在System.ComponentModel.DataAnnotations命名空间中可用

However, if you want to continue using the DatabaseGeneratedAttribute. 但是,如果要继续使用DatabaseGeneratedAttribute。 The error is pretty self explanatory. 该错误很容易解释。 It tells you that it is available in both namespaces 它告诉您在两个名称空间中都可用

System.ComponentModel.DataAnnotations     
System.ComponentModel.DataAnnotations.Schema

You will need to explicity state the namespace you need to use Eg 您将需要明确声明使用Eg所需的名称空间

[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]

You can always use an alias to keep the namespace short and sweet. 您始终可以使用别名来使名称空间简短而优美。

Please check that your project does not have reference to both version of System.ComponentModel.DataAnnotations.dll assembly. 请检查您的项目是否没有同时引用System.ComponentModel.DataAnnotations.dll程序集的两个版本。

Old version (4.0.0.0) can be included to your project by default, and do not be removed after you install package with new version (4.0.10.0). 默认情况下,旧版本(4.0.0.0)可以包含在您的项目中,并且在安装具有新版本(4.0.10.0)的软件包后不会将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM