2013-05-08 92 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類中。就像錯誤消息所說的那樣,實體類型不能嵌套。嘗試將它移到外面,以便它是名稱空間內的頂級類。