System.InvalidOperationException: The type MyContext+Northwind was not mapped.

System.InvalidOperationException: The type MyContext+Northwind 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.

错误原因:

 entity framework不能将内部类与数据库匹配。

错误代码如下所示:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Data.Entity;
 6 using System.ComponentModel.DataAnnotations;
 7
 8 namespace TestCodeFirst
 9 {
10     public class MyContext
11     {
12         public class Product
13         {
14             public int ProductID { get; set; }
15             public int CategoryID { get; set; }
16             [Required(ErrorMessage = "Product Name不能为空")]
17             public string ProductName { get; set; }
18             public Decimal? UnitPrice { get; set; }
19             public bool Discontinued { get; set; }
20             public virtual Category Category { get; set; }
21         }
22
23         public class Category
24         {
25             public int CategoryID { get; set; }
26             public string CategoryName { get; set; }
27             public string Description { get; set; }
28             public byte[] Picture { get; set; }
29             public virtual ICollection<Product> Product { get; set; }
30         }
31         public class Northwind : DbContext
32         {
33             public DbSet<Product> Products { get; set; }
34             public DbSet<Category> Categorys { get; set; }
35         }
36     }
37 }

解决的办法:Northwind作为内部类。正确的代码如下所示

 1 namespace TestCodeFirst
 2 {
 3        public class Product
 4        {
 5            public int ProductID { get; set; }
 6            public int CategoryID { get; set; }
 7              [Required(ErrorMessage="Product Name不能为空")]
 8            public string ProductName { get; set; }
 9            public Decimal? UnitPrice { get; set; }
10            public bool Discontinued { get; set; }
11            public virtual Category Category { get; set; }
12        }
13
14        public class Category
15        {
16            public int CategoryID { get; set; }
17            public string CategoryName { get; set; }
18            public string Description { get; set; }
19            public byte[] Picture { get; set; }
20            public virtual ICollection<Product> Product { get; set; }
21        }
22        public class Northwind:DbContext
23        {
24            public DbSet<Product> Products { get; set; }
25            public DbSet<Category> Categorys { get; set; }
26     }
27 }

 

 

 

作者:kissazi2 
出处:http://www.cnblogs.com/kissazi2/ 
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载:http://www.cnblogs.com/kissazi2/archive/2013/01/04/2843944.html

时间: 2024-08-04 11:52:19

System.InvalidOperationException: The type MyContext+Northwind was not mapped.的相关文章

关于异常的疑难解答:System.InvalidOperationException

问题描述 实例失败.说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.InvalidOperationException:实例失败.源错误:行109:SqlDataAdapterdbAdapter=newSqlDataAdapter(sQueryString,con);行110:DataSetdataset=newDataSet();行111:dbAdapter.Fill(dataset,Table

“System.InvalidOperationException”类型的异常在 EntityFramework.dll 中发生,但未在用户代码中进行处理

问题描述 "System.InvalidOperationException"类型的异常在EntityFramework.dll中发生,但未在用户代码中进行处理其他信息:无法确定类型"Dongxun.CMS.Data.Exam.TableModel.Constraint"的组合主键排序.请使用ColumnAttribute(请参阅http://go.microsoft.com/fwlink/?LinkId=386388)或HasKey(请参阅http://go.mi

System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。

问题描述 如标题,System.InvalidOperationException:超时时间已到.超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小.这个要怎么解决?? 解决方案 解决方案二:高手们,帮帮小妹喽--解决方案三:及时关闭连接,在连接字符串增加maxploor数量.解决方案四:这个要怎么弄?我用vs2005打包后,登录我程序的时候就会提示这个错误,需要怎么来处理?

异常详细信息: System.InvalidOperationException: 在应用程序配置中未找到连接名“testConnectionString3”或者连接字符串为空。

问题描述 大家好,小弟在用asp.net时遇到以下问题:向页面增加一个DropDownList控件时,使用数据绑定连接到数据库,在vs2005测试中能够顺利进行,但是发布到iis上,就不能运行.报错:"/test"应用程序中的服务器错误.--------------------------------------------------------------------------------在应用程序配置中未找到连接名"testConnectionString3"

[System.InvalidOperationException] = {&quot;阅读器关闭时尝试调用 MetaData 无效。&quot;}

 本错误的环境是.Net4.0,数据库访问使用LinQ.本例在数据库访问的类里面将DBAccessDataContext (数据库上下文)做为类变量,后面的数据库操作函数都使用这个类变量,在多线程的环境下面就爆出了[System.InvalidOperationException] = {"阅读器关闭时尝试调用 MetaData 无效."} 的错误. 具体的代码见下方 作为类变量的DBAccessDataContext 1 public class test1 { 2 DBAccess

C# 服务部署 报System.InvalidOperationException异常该怎么处理?

问题描述 Runningatransactedinstallation.BeginningtheInstallphaseoftheinstallation.SeethecontentsofthelogfilefortheD:DonetProjectLH.CPCD_CheckLH.CPCD_Check.exeassembly'sprogress.ThefileislocatedatD:DonetProjectLH.CPCD_CheckLH.CPCD_Check.InstallLog.Anexcep

未处理的“System.InvalidOperationException”类型的异常出现在system.web.services.dll中。其他信息: 客户端发现响应内容类型为“text/

问题描述 "C:WINDOWSMicrosoft.NETFrameworkv1.1.4322TemporaryASP.NETFileshuike.webservice205d4ef74c400ce7"下没有webservice编译生成后的dll文件,生成后应该有这样后缀名的文件.cs.cmdline.dll.err.out.pdb六个文件,但是现在我的电脑里打死不生成.dll的那个文件,程序运行不起来.请帮我看下下面的报错信息,到底是何问题啊?我今天要交任务的,谁知不能运行起来,我快气

c# 导入Excel07时,“引发System.InvalidOperationException”

问题描述 导入Excel时,在conn.Open()时,出现"con.ServerVersion引发System.InvalidOperationException"异常,从而Open()失败,报错的意思是说版本不对,求指点!导入代码如下:stringstrConn="Provider=Microsoft.Jet.OLEDB.4.0;"+"DataSource="+Path+";"+"ExtendedProperti

dataGridView 的硬伤 System.InvalidOperationException

问题描述 在Update后再帮定dataGridView做刷新就抛出System.InvalidOperationException这个垃圾了看了很多帖子都没找到解决办法请问各位高手有无解决办法?只想说dataGridView真垃圾如果上天再给一次机会不会再用他了vsflex8要比这个垃圾好用的多咯 解决方案 解决方案二:跟了一下大概是刷新以后找不到当前活动单元格什么的郁闷的要死搞了一下午....还是那死样解决方案三:顶到高手来接分解决方案四:高手接分解决方案五:我不是高手,没遇到这个问题,呵