System.InvalidOperationException: The type MyContext+Northwind was not mapped.
2013-01-04 12:17
489 查看
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不能将内部类与数据库匹配。
错误代码如下所示:
解决的办法:Northwind作为内部类。正确的代码如下所示
错误原因:
entity framework不能将内部类与数据库匹配。
错误代码如下所示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace TestCodeFirst { public class MyContext { public class Product { public int ProductID { get; set; } public int CategoryID { get; set; } [Required(ErrorMessage = "Product Name不能为空")] public string ProductName { get; set; } public Decimal? UnitPrice { get; set; } public bool Discontinued { get; set; } public virtual Category Category { get; set; } } public class Category { public int CategoryID { get; set; } public string CategoryName { get; set; } public string Description { get; set; } public byte[] Picture { get; set; } public virtual ICollection<Product> Product { get; set; } } public class Northwind : DbContext { public DbSet<Product> Products { get; set; } public DbSet<Category> Categorys { get; set; } } } }
解决的办法:Northwind作为内部类。正确的代码如下所示
namespace TestCodeFirst { public class Product { public int ProductID { get; set; } public int CategoryID { get; set; } [Required(ErrorMessage="Product Name不能为空")] public string ProductName { get; set; } public Decimal? UnitPrice { get; set; } public bool Discontinued { get; set; } public virtual Category Category { get; set; } } public class Category { public int CategoryID { get; set; } public string CategoryName { get; set; } public string Description { get; set; } public byte[] Picture { get; set; } public virtual ICollection<Product> Product { get; set; } } public class Northwind:DbContext { public DbSet<Product> Products { get; set; } public DbSet<Category> Categorys { get; set; } } }
相关文章推荐
- System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
- Ajax ToolKit --- ModelPopupExtender报错:Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomE
- 微软企业库中数据库访问代码问题(System.InvalidOperationException: Parameter '@FileContent' exceeds the size limit for the sql_variant datatype.)
- c# 调用MD5CryptoServiceProvider出现 System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
- System.InvalidOperationException: Operation is not valid due to the current state of the object.
- 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法
- System.InvalidOperationException: The stream does not support reading
- System.InvalidOperationException: The Microsoft.Jet.OLEDB.4.0 provider is not
- 报错:System.InvalidOperationException Could not locate Razor Host Factory type: System.Web.Mvc.MvcWebR
- WCF分布式开发常见错误解决(7):System.InvalidOperationException,Cannot have two operations in the same contract
- System.InvalidOperationException: 缓冲操作当前正在进行中,无法释放 BufferedGraphicsContext。
- System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException: Operation is not valid due to the current state of the obj
- Eclipse上JPA报错:Target Entity is not a entity 和the "mapped by" attribute has an invalid mapping type
- the type xxx was not mapped
- System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- TypeLoadException - Could not load type because the format is invalid
- InvalidOperationException: Operation is not valid due to the current state of the object
- Error: java.lang.UnsupportedOperationException: Not implemented by the HftpFileSystem FileSystem imp
- System.InvalidOperationException nested transactions are not supported
- System.Security.SecurityException The source was not found, but some or all event logs could not be searched.Inaccessible logs Security.