locked
Entity Framework 5.0.0.0 Exception "The type '...' was not mapped." RRS feed

  • Question

  • I followed the demonstration found at http://msdn.microsoft.com/en-us/data/jj193542 to a "T" but I get the following error when I run it.

    The type 'CodeFirstNewDatabaseSample.Program+Blog' 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.

    This happens on the line with the using statement which states...

    using (var db = new BlogContext())

    I am using "Visual Studio Express 2012 for Windows Desktop" on a Windows 8 Operating System. I was able to install the Entity Framework 5.0.0 successfully and everything compiles as expected. But, for some reason, I get this exception when I run it.

    Am I missing something?


    Paul Cardenas

    Monday, March 4, 2013 3:12 AM

Answers

  • I just did a final test and found the possible reason why you are getting the error message you are getting, with this test I also get the same error. Please read the comment just above the Blog class. This code is working.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    
    namespace CodeFirstNewDatabaseSample
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                using (var db = new BloggingContext())
                {
                    // Create and save a new Blog
                    Console.Write("Enter a name for a new Blog: ");
                    var name = Console.ReadLine();
    
                    var blog = new Blog {Name = name};
                    db.Blogs.Add(blog);
                    db.SaveChanges();
    
                    // Display all Blogs from the database
                    var query = from b in db.Blogs
                                orderby b.Name
                                select b;
    
                    Console.WriteLine("All blogs in the database:");
                    foreach (var item in query)
                    {
                        Console.WriteLine(item.Name);
                    }
    
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadKey();
                }
            }
        }
    
        // Please note that the following three classes are outside of the above Main class
        // When you include the below three classes inside the above MAin class the below three
        // classes become sub-classes of the Main class and is NOT what you want to do.
        // In your program the last } above is located after the three classes below which is incorrect.
        
        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 Blog Blog { get; set; }
        }
        
        public class BloggingContext : 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".

    Monday, March 4, 2013 2:43 PM

All replies

  • On 3/3/2013 10:12 PM, Paul A. Cardenas wrote:

    LOL! I am not laughing at you I am just laughing at these so called little quicky tutorials that never work, and one has to dig-in to make them work. :)

    I started out at about 8 pm Saturday night, and I didn't get the "15 minute Walkthrough" tutorial to work until about 4am Sunday. :) That was this past weekend. So I feel your pain.

    <http://msdn.microsoft.com/en-us/library/ff699490.aspx>

    You must have missed something, like I missed a couple of things that were never mentioned. :)

    I did a Code First after doing a Database First tutorial and worked with EF Database First professionally for a couple of years. I followed that Code First tutorial to the "t", and the damn thing never worked after redoing the tutorial a couple of times. Thank god for the DB first tutorial. :) But these little tutorials are mess! :)

    Monday, March 4, 2013 4:30 AM
  • Hi Paul;

    I just went through the first four steps of the the walkthrough and after completing step four I ran the program to do "Reading & Writing Data" and it worked without issue. Seeming this is a short Console program and all code is in one file can you post the text from that file including the using statements at the top.

      


    Fernando (MCSD)

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

    Monday, March 4, 2013 2:30 PM
  • I just did a final test and found the possible reason why you are getting the error message you are getting, with this test I also get the same error. Please read the comment just above the Blog class. This code is working.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    
    namespace CodeFirstNewDatabaseSample
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                using (var db = new BloggingContext())
                {
                    // Create and save a new Blog
                    Console.Write("Enter a name for a new Blog: ");
                    var name = Console.ReadLine();
    
                    var blog = new Blog {Name = name};
                    db.Blogs.Add(blog);
                    db.SaveChanges();
    
                    // Display all Blogs from the database
                    var query = from b in db.Blogs
                                orderby b.Name
                                select b;
    
                    Console.WriteLine("All blogs in the database:");
                    foreach (var item in query)
                    {
                        Console.WriteLine(item.Name);
                    }
    
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadKey();
                }
            }
        }
    
        // Please note that the following three classes are outside of the above Main class
        // When you include the below three classes inside the above MAin class the below three
        // classes become sub-classes of the Main class and is NOT what you want to do.
        // In your program the last } above is located after the three classes below which is incorrect.
        
        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 Blog Blog { get; set; }
        }
        
        public class BloggingContext : 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".

    Monday, March 4, 2013 2:43 PM
  • I am embarrased to say that Fernando was correct! I was putting my classes within the Program class. When I moved them outside of the Program class, sure enough the program worked. Thanks Fernando!

    It reminds me of the time I put in an "=" instead of a "==" inside an if-else statement. Geez! Time to look for another profession. :(


    Paul Cardenas


    Monday, March 4, 2013 7:13 PM
  • Hi Paul;

    Not a problem Paul, glad I was able to help. And as for those pesky little curly brackets and = signs that finds themselves in the wrong place, it happens to the best of us. ;=)

      


    Fernando (MCSD)

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

    Monday, March 4, 2013 8:36 PM
  • Hi,

    Made the same mistake while doing the same sample , guess it was because I was just hitting F5 after writing the doe along with the video and I didn't pay much attention to were I wrote the code :) do you are not the only one making this mistake !

    Monday, April 15, 2013 4:50 PM
  • I'm glad I found this post... I did the same thing, I placed all my classes inside the Program class too. I always code classes in separate files, when creating my own code and didn't notice...

    Thanks!

    Thursday, May 2, 2013 10:30 PM
  • @M3NTA7;

    Glad to hear that you found the post beneficial, 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 3, 2013 3:10 AM