2013-05-08 72 views
2

我试图根据我可以找到的所有教程运行一个简单的程序。 这是我的测试代码:该类型未映射

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

这是在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> 

当我执行的代码,在使用(VAR上下文=新DatabaseContext ())行,我得到以下错误:

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.

请忽略名称MySQL罪ce我并不想将它连接到MySQL,但它为 与localdb一起工作。我四处搜寻,无法解决问题。

回答

4

我认为这是失败的,因为你的Forum类嵌套您Program类中。就像错误消息所说的那样,实体类型不能嵌套。尝试将它移到外面,以便它是名称空间内的顶级类。