2016-12-03 96 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和選擇合適的包裝和安裝。

相關問題