none
Need help with Code First Entity Framework RRS feed

  • Question

  • Greetings,

      I am new to entity framework and code first.  I just went through the following tutorial.

    http://msdn.microsoft.com/en-us/data/jj193542

    I entered the code exactly as it is in the video but I am getting the follow exception when it is executing the first line of the using statement "using (var db = new BlogContext())"

    InvalidOperationException type ' CodeFirstNewDatabaseSample.Program.Blog' was not mapped.

    Thanks

    Thursday, May 23, 2013 7:19 PM

Answers

  • Hi Jon;

    Simple issue to correct. When you created the three classes Blog, Post and BlogContext you created them as internal classes to Class Program and when Entity Framework went to create them they were not able to find them because they were hidden by class Program. Move the } next to last line just before class Blog as shown below.

    namespace CodeFirst1
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                using (var db = new BlogContext())
                {
                    Console.WriteLine("enter name");
                    var name = Console.ReadLine();
    
                    var blog = new Blog() {Name = name};
                    db.Blogs.Add(blog);
                    db.SaveChanges();
    
                    var q = from b in db.Blogs
                            orderby b.Name
                            select b;
    
                    foreach (var item in q)
                    {
                        Console.WriteLine(item.Name);
                    }
    
                    Console.ReadLine();
                }
            }
        }  // This line was moved from the next to last line in the file to here
    
        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 string Content { get; set; }
            public int BlogID { get; set; }
            public virtual List<Blog> Blogs { get; set; }
        }
    
        public class BlogContext : DbContext
        {
            public DbSet<Blog> Blogs { get; set; }
            public DbSet<Post> Posts { get; set; }
        }
    }

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by jon.nowick Friday, May 24, 2013 4:15 PM
    Friday, May 24, 2013 3:14 PM

All replies

  • Check your protected override void OnModelCreating(DbModelBuilder modelBuilder) in the context file. Does it have the mapping to the Blog table?
    Thursday, May 23, 2013 7:42 PM
  • I am on the first part of the that tutorial.  My code does not have any overriden OnModelCreating method.  I did notice that in VS2010 in the server explore that it is creating the database but it is not creating the tables.  In VS2012 it is not even creating the new database.  Is there a configuration setting that I am missing?

    Thursday, May 23, 2013 8:15 PM
  • Did you do this step?

    3. Create a Context

    • Project –> Manage NuGet Packages…
      Note: If you don’t have the Manage NuGet Packages… option you should install the  latest version of NuGet
    • Select the Online tab
    • Select the EntityFramework package
    • Click Install

    Thursday, May 23, 2013 9:31 PM
  • What step did you complete to? 

    Was the last step you completed, "4. Reading & Writing Data" ?

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, May 24, 2013 1:34 AM
  • Thanks for the help so far.  I would really like to figure this out so I can learn and start using Code First. 

    I got as far as the step in the video where he runs the application.  I entered the code exactly as he did.  I used NuGet to install Entity.  The application compiles it just does not create the new database correctly and throws an exception on the first line of the using statement.

    Friday, May 24, 2013 12:21 PM
  • Hi Jon;

    If you would like I will take a look at your project to see what is going on. If you would please zip up your complete project and upload to a public folder on you SkyDrive and post a link here so I can download.

    Have a great day.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, May 24, 2013 1:16 PM
  • Fernando,

      Thanks for your help.  Here is the link to my zipped up project.

    https://skydrive.live.com/redir?resid=A3854B401E83D85F!2165&authkey=!AKUuGouV2rSDQ2M

    Friday, May 24, 2013 2:35 PM
  • Hi Jon;

    Simple issue to correct. When you created the three classes Blog, Post and BlogContext you created them as internal classes to Class Program and when Entity Framework went to create them they were not able to find them because they were hidden by class Program. Move the } next to last line just before class Blog as shown below.

    namespace CodeFirst1
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                using (var db = new BlogContext())
                {
                    Console.WriteLine("enter name");
                    var name = Console.ReadLine();
    
                    var blog = new Blog() {Name = name};
                    db.Blogs.Add(blog);
                    db.SaveChanges();
    
                    var q = from b in db.Blogs
                            orderby b.Name
                            select b;
    
                    foreach (var item in q)
                    {
                        Console.WriteLine(item.Name);
                    }
    
                    Console.ReadLine();
                }
            }
        }  // This line was moved from the next to last line in the file to here
    
        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 string Content { get; set; }
            public int BlogID { get; set; }
            public virtual List<Blog> Blogs { get; set; }
        }
    
        public class BlogContext : DbContext
        {
            public DbSet<Blog> Blogs { get; set; }
            public DbSet<Post> Posts { get; set; }
        }
    }

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by jon.nowick Friday, May 24, 2013 4:15 PM
    Friday, May 24, 2013 3:14 PM
  • BALLS!  

    I knew it had to be something simple like that.  Thanks so much for your help!

    Friday, May 24, 2013 4:15 PM
  •  

    Not a problem, glad I was able to help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, May 24, 2013 4:17 PM