none
System.InvalidOperationException in every program with EF RRS feed

  • Question

  • Hello. I am trying to compile this code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    
    
    namespace CodeFirstNewDatabaseSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                using (var db = new BloggingContext())
                {
                    //...
                }
            }
    
            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; }
            }
        }
    }
    

    But I get the next exception

    System.InvalidOperationException не обработано
      HResult=-2146233079
      Message=The type 'CodeFirstNewDatabaseSample.Program+Person' 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.
      Source=EntityFramework
      StackTrace:
           в System.Data.Entity.Internal.DbSetDiscoveryService.GetSets()
           в System.Data.Entity.Internal.DbSetDiscoveryService.InitializeSets()
           в System.Data.Entity.DbContext.DiscoverAndInitializeSets()
           в System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
           в System.Data.Entity.DbContext..ctor()
           в CodeFirstNewDatabaseSample.Program.MyContext..ctor() в c:\Users\USER\Documents\Visual Studio 2012\Projects\EfSamples\CodeFirstNewDatabaseSample\CodeFirstNewDatabaseSample\Program.cs:строка 40
           в CodeFirstNewDatabaseSample.Program.Main(String[] args) в c:\Users\USER\Documents\Visual Studio 2012\Projects\EfSamples\CodeFirstNewDatabaseSample\CodeFirstNewDatabaseSample\Program.cs:строка 22
           в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           в System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    Sorry for russian languge, but I think you shall understand it.

    My configuration file is

    <?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.SqlConnectionFactory, EntityFramework" />
      </entityFramework>
    </configuration>

    I think something wrong with Visaul Studio 2012 and I used repair. but it did not help.

    I sent my code to my friend and he run it succesfully.

    Please, help me to fix it, I can't start learning EF =(

    Saturday, April 13, 2013 5:47 PM

Answers

  • Hi mmm1808;

    I think what your problem is that you made the three EF POCO classes Blog, Post and BloggingContext as internal classes to the class program and therefore can not be found. Try formatting the code as shown below placing the three EF classes at the same level as the Program class.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    
    
    namespace CodeFirstNewDatabaseSample
    {
        class Program
        {
            static void Main(string[] args)
            {            
                using (var db = new BloggingContext())
                {
                    //...
                }
            }
        }
    
        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".

    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 mmm1808 Saturday, April 13, 2013 7:39 PM
    Saturday, April 13, 2013 7:09 PM

All replies

  • Hi mmm1808;

    I think what your problem is that you made the three EF POCO classes Blog, Post and BloggingContext as internal classes to the class program and therefore can not be found. Try formatting the code as shown below placing the three EF classes at the same level as the Program class.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    
    
    namespace CodeFirstNewDatabaseSample
    {
        class Program
        {
            static void Main(string[] args)
            {            
                using (var db = new BloggingContext())
                {
                    //...
                }
            }
        }
    
        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".

    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 mmm1808 Saturday, April 13, 2013 7:39 PM
    Saturday, April 13, 2013 7:09 PM
  • Thank you very miuch.

    What a stupid mistake... Need to be more attentive.

    Saturday, April 13, 2013 7:40 PM
  •  

    We all do things like that and a good reason for forums like this to have someone look over the code. 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.

    Sunday, April 14, 2013 2:12 AM