简体   繁体   English

类型未映射的实体框架5.0

[英]The type not mapped entity framework 5.0

I was following a video on EF code first . 我首先关注有关EF代码的视频

I tried to follow the exact steps in VS 2012 console app, Please see my code below, 我试图按照VS 2012控制台应用程序中的确切步骤操作,请参阅下面的代码,

class Program
{
    static void Main(string[] args)
    {
        using (var context = new EftDbContext())
        {
            context.Blogs.Add(new Blog { Name = "ASP.NET" });
            context.SaveChanges();

            var blogs = from n in context.Blogs
                        orderby n.Name
                        select n;

            foreach (var b in blogs)
            {
                Console.WriteLine(b);
            }
        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }

        public virtual List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class EftDbContext : DbContext
    {
        public EftDbContext()
            : base("name=EftDbContext")
        {

        }
        public DbSet<Post> Posts { get; set; }
        public DbSet<Blog> Blogs { get; set; }
    }
}

However when I run my application it give me following error: 但是,当我运行我的应用程序时,它会给我以下错误:

The type 'EntityFrameworkTest.Program+Post' was not mapped. 未映射“EntityFrameworkTest.Program + Post”类型。 Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. 通过使用Ignore方法或NotMappedAttribute数据批注检查类型是否未明确排除。 Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject. 验证类型是否定义为类,不是原始的,嵌套的或泛型的,并且没有从EntityObject继承。

在此输入图像描述

If someone interested here is my current connection string (I tried multiple option): 如果有兴趣的人是我当前的连接字符串(我试过多个选项):

  <connectionStrings>
    <add name="EftDbContext"
     connectionString="Database=TestDB;Server=(local);Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>

Where do I need to specify the mapping and more importantly why? 我需要在哪里指定映射,更重要的是为什么? EF should pick that up no? EF应该选择不?

I have created another project using VS 2012 web API template and that did not show up this error... 我使用VS 2012 Web API模板创建了另一个项目,但没有显示此错误...


Update: 更新:

DB does not exist. DB不存在。 I am yet to execute this application successfully for the first time on my machine so that EF code first generate the database for me. 我尚未在计算机上首次成功执行此应用程序,因此EF代码首先为我生成了数据库。 It is failing in the EftDbContext constructor itself. 它在EftDbContext构造函数本身中失败了。


Update 2: This problem is solved. 更新2:此问题已解决。 The issue was all my POCO classes and DbContext was nested in Program class. 问题是我的所有POCO类和DbContext都嵌套在Program类中。 EF does not like it somehow... EF不喜欢它...

From comments above: 从上面的评论:

Your Blog and Post classes, as well as your dbcontext are all declared within your Program class. 您的Blog和Post类以及dbcontext都在Program类中声明。 They should not be. 他们不应该。

EF5 does not support nested types. EF5不支持嵌套类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM