ASP.NET vNext DataAnnotations DatabaseGenerated 不工作
ASP.NET vNext DataAnnotations DatabaseGenerated not working
我正在尝试在 ASP.NET 5:
中定义这样的模型 ID 变量
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
为了支持数据注释,我在我的 project.json 文件中添加了包 System.ComponentModel.DataAnnotations
,如下所示:
"System.ComponentModel.Annotations": "4.0.10-beta-22811"
我在模型 cs 文件中添加了 using System.ComponentModel.DataAnnotations.Schema;
虽然我收到以下错误:
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'
而且我真的不知道该如何解决这个问题。我试图包含命名空间 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?)
如果那个命名空间不存在,我不明白我怎么能得到之前的错误,它告诉我 DatabaseGeneratedAttribute 存在于两个地方。
我真的很感激我能得到的所有帮助。
您可以只使用KeyAttribute。那应该为您自动生成。
[Key]
public int Id { get; set; }
此属性在 System.ComponentModel.DataAnnotations 命名空间中可用
但是,如果您想继续使用 DatabaseGeneratedAttribute。该错误是不言自明的。它告诉你它在两个命名空间中都可用
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations.Schema
您需要明确说明您需要使用的命名空间,例如
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
您始终可以使用 别名 来保持命名空间简短而甜美。
请检查您的项目没有引用两个版本的 System.ComponentModel.DataAnnotations.dll 程序集。
旧版本(4.0.0.0)可以默认包含到您的项目中,并且在安装新版本(4.0.10.0)的包后不会被删除。
我正在尝试在 ASP.NET 5:
中定义这样的模型 ID 变量[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
为了支持数据注释,我在我的 project.json 文件中添加了包 System.ComponentModel.DataAnnotations
,如下所示:
"System.ComponentModel.Annotations": "4.0.10-beta-22811"
我在模型 cs 文件中添加了 using System.ComponentModel.DataAnnotations.Schema;
虽然我收到以下错误:
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'
而且我真的不知道该如何解决这个问题。我试图包含命名空间 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?)
如果那个命名空间不存在,我不明白我怎么能得到之前的错误,它告诉我 DatabaseGeneratedAttribute 存在于两个地方。
我真的很感激我能得到的所有帮助。
您可以只使用KeyAttribute。那应该为您自动生成。
[Key]
public int Id { get; set; }
此属性在 System.ComponentModel.DataAnnotations 命名空间中可用
但是,如果您想继续使用 DatabaseGeneratedAttribute。该错误是不言自明的。它告诉你它在两个命名空间中都可用
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations.Schema
您需要明确说明您需要使用的命名空间,例如
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
您始终可以使用 别名 来保持命名空间简短而甜美。
请检查您的项目没有引用两个版本的 System.ComponentModel.DataAnnotations.dll 程序集。
旧版本(4.0.0.0)可以默认包含到您的项目中,并且在安装新版本(4.0.10.0)的包后不会被删除。