0

I've extended my ASP.Net Web Pages application with the Entity Framework, which I have downloaded in Visual Studio via NuGet. Now I get the following error, while trying to assign a different column name to a propertie.

CS0246: The type or namespace name 'Column' could not be found (are you missing a using directive or an assembly reference?)

My Model:

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; }
    }
}

The ColumnAttribute should be in System.ComponentModel.DataAnnotations.Schema, so I can't understand the problem.

UPDATE My Web.Config file:

<?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>
android
  • 652
  • 9
  • 28
  • the attribute is under System.ComponentModel.DataAnnotations – Prabhu Murthy Apr 29 '13 at 16:19
  • I think it depends on the version of EF he is using, it used to live in there but now it is in System.ComponentModel.DataAnnotations.Shema (as of EF 5 I think). I would check that you are targeting .NET 4.5 and are referencing the latest version of EF. – blins Apr 29 '13 at 16:32
  • I just realized that even though NuGet told me otherwise, I have EF 4.4 installed instead of EF 5. Please take a look at my Web.Config. Might that be the problem? – android Apr 30 '13 at 06:43

2 Answers2

2

in Entity Framework 5, ColumnAttribute is moved class to a different namespace called System.ComponentModel.DataAnnotations.Schema. So you need to add a using statement to include that namespace too in your model class if you are using EF 5

 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23
  • As you can see in my code, I already did that. But I just discovered that I might have the wrong EF version installed. Could that be the problem? – android Apr 30 '13 at 06:45
  • I just tried it but it didn't work. In my packages.config I've found this: `` What version is now installed? 4.4 or 5? I don't get it... – android Apr 30 '13 at 06:52
  • its version 5.. try this command from console PM> Install-Package EntityFramework -Version 5.0.0 – Shafqat Masood Apr 30 '13 at 06:56
  • `'EntityFramework 5.0.0' already installed. Successfully added 'EntityFramework 5.0.0' to Pezag.Net.` But it's still not working. – android Apr 30 '13 at 07:01
  • Some features are only available when writing an application that targets .NET 4.5. This includes enum support, spatial data types, table-valued functions and the performance improvements. – Shafqat Masood Apr 30 '13 at 07:25
  • This version of the NuGet package is fully compatible with Visual Studio 2010 and Visual Studio 2012 and can be used for applications targeting .NET 4.0 and 4.5. – Shafqat Masood Apr 30 '13 at 07:27
  • Okay thanks. So any idea why it's still not working? It shouldn't be a problem that I override the Initializer, right? `System.Data.Entity.Database.SetInitializer(null);` – android Apr 30 '13 at 07:34
  • for that please check [this](http://stackoverflow.com/questions/6368553/entity-framework-database-setinitializer-simply-not-working) – Shafqat Masood Apr 30 '13 at 07:40
  • That seems to be a different problem. Anyway, overriding the Initializer doesn't seem to be my problem, since it's working fine without the ColumnAttribute in my Model. So any idea what's wrong? – android Apr 30 '13 at 07:54
2

After upgrading my application to .Net 4.5 it's finally working as expected. It looks like EF 5 isn't fully compatible with .Net 4.0.

android
  • 652
  • 9
  • 28