eddyuk
eddyuk

Reputation: 4190

The type was not mapped

Im trying to run a simple program according to all tutorial I could find. This is my test code:

Program.cs:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF5WithMySQLCodeFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var context = new DatabaseContext())
            {
                var defaultForum = new Forum { ForumId = 1, Name = "Default", Description="Default forum to test insertion." };
                context.Forums.Add(defaultForum);
                context.SaveChanges();
            }
            Console.ReadKey();
        }

        public class Forum
        {
            public int ForumId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }

        public class DatabaseContext : DbContext
        {
            public DbSet<Forum> Forums { get; set; }
        }
    }
}

This is the 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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" 
                      sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

When I execute the code, in using(var context = new DatabaseContext()) line, I get the following error:

The type 'EF5WithMySQLCodeFirst.Program+Forum' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

Please ignore the name MySQL since I am not trying yet to connect it to MySQL but for it to work with localdb. I searched around and can't get the solution for problem yet.

Upvotes: 2

Views: 3502

Answers (1)

Jeremy Todd
Jeremy Todd

Reputation: 3289

I think it's failing because your Forum class is nested inside your Program class. Like the error message says, entity types can't be nested. Try moving it outside so that it's a top-level class within the namespace.

Upvotes: 4

Related Questions