2013-03-10 130 views
4

有一些MVC應用程序的代碼,它是使用.NET 4.5作爲VS 2012中的框架構建的。我目前的系統迫使我在VS 2010上工作。我設法打開我的VS2010中的VS2012解決方案,但問題是VS2010只支持.NET 4..NET 4.0替代.NET 4.5命名空間

代碼中有幾個函數只使用.NET 4.5,例如System.ComponentModel.DataAnnotations.Schema

那麼,有沒有.NET 4中可用的替代函數/屬性,我現在可以使用它來完成與.NET 4.5相同的功能?

這是使用.NET 4.5我當前的代碼:

[Table("UserProfile")] 
    public class UserProfile 
    { 
     [Key] 
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
     public int UserId { get; set; } 
     public string UserName { get; set; } 
    } 

正如你可以看到DatabaseGeneratedAttributeSystem.ComponentModel.DataAnnotations.Schema命名空間,這是.NET 4.5的一部分得到。

有關.NET 4中可用於表示相同邏輯的相應函數/屬性的任何建議?

注:在代碼片段上面給出的,我得到Table錯誤和DatabaseGeneratedAttribute作爲

類型或命名空間名稱「表」找不到(是否缺少using指令或程序集引用?)

類型或命名空間名稱「DatabaseGener atedAttributeAttribute'找不到(您是否缺少使用指令或程序集引用?)

。所以,我猜測我只需要在NET 4.0中找到相應的類,事情就會落實到位。您的幫助深表謝意。

+0

這些註釋[是實體框架5.0的一部分](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v = vs.103).aspx)。 (也許更早的版本,我不知道。)無論哪種方式,EF5應該與.NET 4.0一起工作,所以只需將其添加到您的項目。 – millimoose 2013-03-10 23:25:23

+0

但是,先生,我已經在我的項目中引用了EF 5。這裏是參考'Assembly EntityFramework -D:\ Programming \ MVC \ PassMe \ packages \ EntityFramework.5.0.0 \ lib \ net45 \ EntityFramework.dll' – Swayam 2013-03-10 23:31:30

+0

而這裏http://msdn.microsoft.com/en-us/ library/system.componentmodel.dataannotations.schema.databasegeneratedattribute.aspx,它顯示在.NET 4.5版本中可用。我錯過了什麼嗎? – Swayam 2013-03-10 23:37:39

回答

9

從.NET 4.5更改爲4.0後,我看到相同的問題。根據this article「有一個特殊版本的實體框架程序集」用於.NET 4.0,其中包含DataAnnotations,它們已被合併到.NET 4.5中。

重新安裝實體框架使得System.ComponentModel.DataAnnotations.Schema再次工作。通過在軟件包管理器控制檯中鍵入以下內容來重新安裝:

Uninstall-Package EntityFramework 
Install-Package EntityFramework 
+0

這是有效的,我檢查了VS2015 – 2016-02-04 14:49:39

4

this article中,作者聲稱能夠在使用VS2010 SP1的MVC4應用程序中使用System.ComponentModel.DataAnnotations.Schema

讓我們知道這是否適合你。

1

Henrik的解決方案也適用於相反的情況。我從.net 4.0轉到.net 4.5.1以支持MVC 5.2.3。我得到了同樣的錯誤。在這種情況下卸載並重新安裝實體框架也解決了問題:

Uninstall-Package EntityFramework 
Install-Package EntityFramework 
+0

感謝您對相反情況的澄清! :) – Swayam 2016-05-02 19:50:13

0

軟件包管理器控制檯不適用於我。我不得不去管理Nuget包來解決這個問題。我正在使用VS.net 2013