2016-12-03 97 views
0

我是asp.net mvc的新用户,第一次创建新的Web应用程序并在我的项目解决方案中右键单击并将新项目添加到解决方案并将类库添加到我的项目中,并将其命名为Datalayer,因此在Datalayer中创建新类并将其命名PageGroup,在PageGroup要添加System.ComponentModel.DataAnnotations命名空间,但得到这个错误:为什么我不能在我的课堂中使用System.ComponentModel.DataAnnotations?

A namespace cannot directly contain members such as fields or methods...

我的实体框架的工作添加到datalayer但这样不行,我怎样才能解决这个问题?
这是我的类代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.ComponentModel; 

namespace Datalayer 
{ 
    public class PageGroup 
    { 
     [Key] 
     public int GroupID { get; set; } 
    } 
} 


[Key]出现错误。

+0

显示你的代码。你显然有一些不在课堂内的声明 –

+0

@StephenMuecke我的朋友,你为什么投票给我!我说的是在asp.net mvc的新 –

+0

@StephenMuecke请稍候来更新我的问题 –

回答

0

我想你忘了在你的应用程序中添加组件的参考。 转到参考 - >添加参考 - >选择System.ComponentModel.DataAnnotations程序集到您的应用程序。

然后使用命名空间在应用程序

using System.ComponentModel.DataAnnotations; 

请找到截图以供参考 enter image description here

+0

但是当我在类库右击并添加引用我看不出有任何组件 –

+0

请找我添加了屏幕截图 –

0

添加组件system.ComponentModel.DataAnnotation在当前项目。

转到添加引用>组件>框架和选择system.ComponentModel.DataAnnotation

如果这是不存在于当前的组件,然后下载它形成的NuGet包管理器。你的项目

写点击>管理的NuGet包和搜索System.componentmodel和选择合适的包装和安装。

相关问题