1

I just started to work on WebMatrix, I did this tutorial to learn the basic and then I started with this one to get started with Entity Framework on WebMatrix (Note: That tutorial is for WebMatrix 1, but the only thing I see changed was the way you install helpers into your project). The thing is, I installed the Entity Framework helper, but my project doesn't recognize the EntityFramework.dll.

I get this error The type or namespace name 'MaxLength' could not be found (are you missing a using directive or an assembly reference?). But the EntityFramework.dll does appear in my bin folder, what is going on? I don't understand why the reference isn't working.

This is my Web.config file according to the second tutorial and after installing the EntityFramework helper:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0"><assemblies><add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /><add assembly="System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /></assemblies></compilation>
  </system.web>
  <connectionStrings>
    <add name="BookContext" connectionString="Data Source=|DataDirectory|Books.sdf" providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>
</configuration>

This is the compiler output link

Am I missing something? Why does the EntityFramework.dll isn't being recognized properly by my project?

UPDATE

I thought the problem was solved when I copied the EntityFramework.dll from a webpage project created in Visual Studio 2012, but even though the errors aren't shown anymore, WebMatrix still doesn't recognize annotations like Table, Column, ForeignKey, InverseProperty, etc.

I really don't understand, a simple database is created but I can't use the EntityFramework annotations to specify all I need for the database. WebMatrix should recognize the EntityFramewor.dll but it is not. I need help in this one because I'm really lost.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Uriel Arvizu
  • 1,876
  • 6
  • 37
  • 97
  • MaxLengthAttribute lives in System.ComponentModel.DataAnnotations.dll assembly. http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.maxlengthattribute.length.aspx – Pawel Dec 11 '12 at 23:55
  • Entity Framework also has a MaxLengthAttribute http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.maxlengthattribute%28v=vs.103%29.aspx – Uriel Arvizu Dec 12 '12 at 16:33
  • Yes it used to live in EntityFramework but was then moved to System.ComponentModel.DataAnnotations – Pawel Dec 12 '12 at 17:32
  • then what about annotations like Table, Column, InverseProperty and such, I'm trying to generate the database with these annotations but those aren't being recognized, even though they're from EntityFramework they reside in the System.ComponentModel.DataAnnotations namespace which I don't understand why Microsoft decided to do that. What other alternatives do I have besides EntityFramework? – Uriel Arvizu Dec 13 '12 at 17:54
  • I should add the using directive `using System.ComponentModel.DataAnnotations.Schema;` to your pages. – GmG Dec 13 '12 at 20:35
  • Ok, I tried it and does recognize the annotations, but it's weird, why does it not appear high lighted in light blue like the rest of the annotations? is that normal behavior in WebMatrix? Also I want to know, do I need to add anymore namespaces to use Code First to its full potential? and also, when I launch Visual Studio 2012 from WebMatrix, several errors are marked, for example, the namespace Schema is marked as unknown namespace and the project will not run from Visual Studio because of this, if I want to use Visual Studio to create the UIs, will this suppose an obstacle to me? – Uriel Arvizu Dec 13 '12 at 21:22
  • also could you update your answer so I can mark it as accepted? Thanks @GmG – Uriel Arvizu Dec 13 '12 at 21:24

1 Answers1

2

The error that you see is the end result of the version of EntityFramework installed on your site.

The EntityFramework 5.0 package installs version 4.4 or 5.0 of the EntityFramework.dll depending on the version of .NET (4.0 or 4.5) that the project is targeting.

In my experience, installing EntityFramework from the NuGet Gallery in WebMatrix always copies version 4.4 of EntityFramework.dll into the bin folder: you can check it looking at Properties --> Details of the EntityFramework.dll file.

The only solution I know is to download EntityFramework v.5.0 using VisualStudio Express or another standalone utility (look at this post: Install Entity Framewok v5 in Webmatrix 2 RC) and simply replace the dll installed by NuGet Gallery in WebMatrix with this one.

UPDATE

Other problems experimented with EntityFramework v.5.0 and WebMatrix are that the type 'System.Data.EntityState' is defined in an assembly that is not referenced and that is needed a using directive for DataAnnotations.Schema.

The first problem is resolved adding the following reference to web.config (look at Could not load file or assembly 'System.Data.Entity):

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

The second problem needs the addition of the directive

using System.ComponentModel.DataAnnotations.Schema;

to the code.

Anyway, in my opinion WebMatrix isn't the right tool for developing a site based on EntityFramework.

Community
  • 1
  • 1
GmG
  • 1,372
  • 1
  • 9
  • 10
  • I did as you told and created a project in Visual Studio and copied the EntityFramework.dll from the packages folder, inside the EntityFramework.5.0.0 folder I found a lib folder and inside of it I found two more folders, one named net40 and another one named net45. I copied the dll from the net45 folder into my WebMatrix project and the error stopped appearing, but the database isn't being created nor the EntityFramework attributes being recognized (they're not highlighted like normal attributes), am I missing something else? – Uriel Arvizu Dec 12 '12 at 16:30
  • do I have to add the reference manually somewhere? [this](http://i.imgur.com/R3kgV.png) is what I'm refering when I say the dll isn't being recognized, while the rest of the dlls are being recognized, the EF dll isn't one of them, according to the tutorial, when I run the empty cshtml a databse should be created on the App_Data folder, but as you can see in the image, no database was created, that's why I'm asking if I should add a reference somewhere else. Thanks for the help. – Uriel Arvizu Dec 12 '12 at 16:45
  • Are you sure that the database isn't created when you run your site? If you see in your browser the same output as in the Brind's article and no exception are thrown, the database is in your App_Data folder; still you should refresh your folder tree to see it. – GmG Dec 12 '12 at 16:55
  • ok, it does appear now after I closed and reopened WebMatrix, how weird, but now it works, thanks for your help. – Uriel Arvizu Dec 12 '12 at 17:17
  • just one last question, where should I put the _AppStart.cshtml file that reconstructs the database? because the data isn't being erased everytime I run the webpage – Uriel Arvizu Dec 12 '12 at 17:23
  • The _AppStart.cshtml file must be in the root directory. Its job is to force the creation of a new database every time the model changes and not every time the webpage is displayed. Try adding a new property to a class: when you run the site the database is recreated. – GmG Dec 12 '12 at 17:57
  • ok, now I get it, so I don't need to add a reference to the _AppStart.cshtml? does all files with a name that starts with _ get executed automatically? – Uriel Arvizu Dec 12 '12 at 18:07
  • _AppStart.cshtml and _PageStart.cshtml are special files. If an _AppStart.cshtml file exists in the root of a site, it is executed before the site starts. If a _PageStart.cshtml exists in a folder, it is executed before the execution of any page present in that folder. In the Web Pages framework, pages with leading underscores in their names can't be requested directly. – GmG Dec 12 '12 at 20:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21039/discussion-between-uriel-arvizu-and-gmg) – Uriel Arvizu Dec 12 '12 at 23:07