2013-04-29 127 views
0

我已经使用Entity Framework扩展了我的ASP.Net Web Pages应用程序,该应用程序框架是通过NuGet在Visual Studio中下载的。现在我得到以下错误,同时尝试为属性分配不同的列名称。为EF模型使用ColumnAttribute时出错

CS0246:类型或命名空间名称 '列' 找不到(被 你缺少using指令或程序集引用?)

我的模型:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 
using WebMatrix.Data; 

namespace Models { 
    public class News 
    { 
     public int Id { get; set; } 

     [Column("d_date")] 
     public DateTime Date { get; set; } 

     [Column("m_text")] 
     public string Text { get; set; } 
    } 
} 

ColumnAttribute应该在System.ComponentModel.DataAnnotations.Schema,所以我不明白这个问题。

UPDATE 我的web.config文件:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="MyDatasource" connectionString="server=localhost; database=testdb; User Id=***; Password=***;" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <system.web> 
    <customErrors mode="Off" /> 
    <compilation debug="true" targetFramework="4.0"> 
     <assemblies> 
     <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> 
     </assemblies> 
    </compilation> 
    </system.web> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    </system.webServer> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    </entityFramework> 
</configuration> 
+0

该属性是在System.ComponentModel.DataAnnotations – 2013-04-29 16:19:23

+0

我认为这取决于他使用的EF版本,它曾经住在那里但现在它在System.ComponentModel.DataAnnotations.Shema(EF 5 I认为)。我会检查你的目标是.NET 4.5,并引用最新版本的EF。 – blins 2013-04-29 16:32:17

+0

我刚刚意识到,即使NuGet告诉我,否则,我已经安装了EF 4.4而不是EF 5.请看看我的Web.Config。这可能是问题吗? – android 2013-04-30 06:43:19

回答

2

升级我的应用程序后,.NET 4.5,它终于按预期工作。它看起来像EF 5不完全兼容。NET 4.0。

1

在实体框架5,ColumnAttribute移动类名为System.ComponentModel.DataAnnotations.Schema不同的命名空间。所以,你需要添加一个using语句以包括命名太模型类,如果你使用的是EF 5

using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
+0

正如您在我的代码中看到的,我已经这样做了。但是我发现我可能安装了错误的EF版本。这可能是问题吗? – android 2013-04-30 06:45:52

+0

重新安装EF,看看是否能解决问题 – 2013-04-30 06:47:59

+0

我刚试过,但没有奏效。在我的packages.config中我发现这个:''现在安装了什么版本? 4.4或5?我不明白... – android 2013-04-30 06:52:58