Can't get CodeFirst working (EF 4.3) (used EF 4.2 Code First Walkthrough): InvalidOperationException: Entity not mapped

397 views Asked by At

I'm experimenting with EntityFramework 4.3 using CodeFirst. I have worked with EF 4.0 ModelFirst and DatabaseFirst for about two years now and want to integrate CodeFirst into an exiting CRM Database project. (...but tried it without an existing Database!)

I am using VS 2010, VB.Net ans SQLEXPRESS Server 2008 for developing.

So I started with a NEW project using the msdn-Examples: "EF 4.2 Code First Walkthrough" ( http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx )

and a VB.NET adaption from "Productivity Improvements for the Entity Framework" ( http://blogs.msdn.com/b/vbteam/archive/2010/06/21/productivity-improvements-for-the-entity-framework.aspx )

I quote: "That is 100% of the code you would write to get this program running. No separate model definition, XML metadata, config file or anything else is required. "

Well, that seems not to be true for my system. I installed EntityFramework 4.3 with NuGet Packet Manager and the Reference is correctly there.

I first tried it without App.Config - adaption. I got the InvalidOperationException ... not mapped:

"The type 'CodeFirstSample.MyDataAccessDemo.Program + Product' was not mapped."

I added a database connectionString to the app.config for my SQL Server-connection, created a database "Products" and tried again. Same error...

What did I do wrong?

I googled a lot but couldn't find any hints.

2

There are 2 answers

0
Mostafa On

I suppose you have some problem with Database initialization and Connection string .

It's necessary you define your database initialization strategy at starting point of your project , One example could be :

  Database.SetInitializer(
    new DropCreateDatabaseIfModelChanges<YourContext>());

I do recommend try some of Julie Lerman's Source code example , I assure you it 100% work with sql server 2008 express and visual studio 2008 without touching a line of code , Just hit F5 .

0
Ali On

You have to bring Blog and Post classes out of Main class.

Please look at this post: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/84dd6851-9ee5-48a2-950b-de90cdb30cb0/