热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#学习教程:类型”未映射分享

类型”未映射我一直试图解决这个错误一段时间了。每当我的应用程序尝试创建数据上下文的实例时,我都会收到此错误。以下是代码:usingSystem;usingSystem.

类型”未映射

我一直试图解决这个错误一段时间了。

每当我的应用程序尝试创建数据上下文的实例时,我都会收到此错误。

以下是代码:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using RandomRentals.Models; using System.Data.Entity; namespace RandomRentals.Models { public class RentalContext : DbContext { public DbSet Rentals { get; set; } public DbSet Categories { get; set; } public DbSet Items { get; set; } public DbSet Billings { get; set; } public DbSet Users { get; set; } public DbSet 

这是堆栈跟踪:

 [InvalidOperationException: The type 'RandomRentals.Rental' 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.] System.Data.Entity.Internal.DbSetDiscoveryService.GetSets() +706 System.Data.Entity.Internal.DbSetDiscoveryService.InitializeSets() +31 System.Data.Entity.DbContext.DiscoverAndInitializeSets() +56 System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model) +79 System.Data.Entity.DbContext..ctor() +99 RandomRentals.Models.RentalContext..ctor() +44 RandomRentals.Models.UserModel..ctor() in C:UsersnikkaDesktopRandomRentalsRandomRentalsModelsUserModel.cs:11 [TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241 System.Activator.CreateInstance(Type type, Boolean nonPublic) +69 System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +199 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +572 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +449 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +317 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +117 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 System.Web.Mvc.Controller.ExecuteCore() +116 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 System.Web.Mvc.c__DisplayClassb.b__5() +37 System.Web.Mvc.Async.c__DisplayClass1.b__0() +21 System.Web.Mvc.Async.c__DisplayClass8`1.b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.c__DisplayClasse.b__d() +50 System.Web.Mvc.SecurityUtil.b__0(Action f) +7 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 

这是完整的错误文本:

未映射“RandomRentals.Rental”类型。 使用Ignore方法或NotMappedAttribute数据批注检查未明确排除类型。 validation类型是否已定义为类,不是原始类,嵌套类还是通用类,并且不从EntityObjectinheritance。

编辑:租赁类的代码。

 [EdmEntityTypeAttribute(NamespaceName="DevelopmentModel", Name="Rental")] [Serializable()] [DataContractAttribute(IsReference=true)] 

public partial class Rental:EntityObject {#region Factory Method

 ///  /// Create a new Rental object. ///  /// Initial value of the RentalID property. /// Initial value of the AgreedPrice property. /// Initial value of the RentedOn property. /// Initial value of the RentalDays property. /// Initial value of the Returned property. /// Initial value of the RentedBy property. /// Initial value of the ReturnedOn property. /// Initial value of the OwnerReviewed property. /// Initial value of the BillingID property. /// Initial value of the ItemID property. /// Initial value of the CategoryDescription property. /// Initial value of the Title property. public static Rental CreateRental(global::System.Int32 rentalID, global::System.Decimal agreedPrice, global::System.DateTime rentedOn, global::System.Int32 rentalDays, global::System.Boolean returned, global::System.Int32 rentedBy, global::System.DateTime returnedOn, global::System.Boolean ownerReviewed, global::System.Int32 billingID, global::System.Int32 itemID, global::System.String categoryDescription, global::System.String title) { Rental rental = new Rental(); rental.RentalID = rentalID; rental.AgreedPrice = agreedPrice; rental.RentedOn = rentedOn; rental.RentalDays = rentalDays; rental.Returned = returned; rental.RentedBy = rentedBy; rental.ReturnedOn = returnedOn; rental.OwnerReviewed = ownerReviewed; rental.BillingID = billingID; rental.ItemID = itemID; rental.CategoryDescription = categoryDescription; rental.Title = title; return rental; } #endregion #region Primitive Properties ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 RentalID { get { return _RentalID; } set { if (_RentalID != value) { OnRentalIDChanging(value); ReportPropertyChanging("RentalID"); _RentalID = StructuralObject.SetValidValue(value); ReportPropertyChanged("RentalID"); OnRentalIDChanged(); } } } private global::System.Int32 _RentalID; partial void OnRentalIDChanging(global::System.Int32 value); partial void OnRentalIDChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public Nullable ProposedPrice { get { return _ProposedPrice; } set { OnProposedPriceChanging(value); ReportPropertyChanging("ProposedPrice"); _ProposedPrice = StructuralObject.SetValidValue(value); ReportPropertyChanged("ProposedPrice"); OnProposedPriceChanged(); } } private Nullable _ProposedPrice; partial void OnProposedPriceChanging(Nullable value); partial void OnProposedPriceChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Decimal AgreedPrice { get { return _AgreedPrice; } set { OnAgreedPriceChanging(value); ReportPropertyChanging("AgreedPrice"); _AgreedPrice = StructuralObject.SetValidValue(value); ReportPropertyChanged("AgreedPrice"); OnAgreedPriceChanged(); } } private global::System.Decimal _AgreedPrice; partial void OnAgreedPriceChanging(global::System.Decimal value); partial void OnAgreedPriceChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.DateTime RentedOn { get { return _RentedOn; } set { OnRentedOnChanging(value); ReportPropertyChanging("RentedOn"); _RentedOn = StructuralObject.SetValidValue(value); ReportPropertyChanged("RentedOn"); OnRentedOnChanged(); } } private global::System.DateTime _RentedOn; partial void OnRentedOnChanging(global::System.DateTime value); partial void OnRentedOnChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 RentalDays { get { return _RentalDays; } set { OnRentalDaysChanging(value); ReportPropertyChanging("RentalDays"); _RentalDays = StructuralObject.SetValidValue(value); ReportPropertyChanged("RentalDays"); OnRentalDaysChanged(); } } private global::System.Int32 _RentalDays; partial void OnRentalDaysChanging(global::System.Int32 value); partial void OnRentalDaysChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Boolean Returned { get { return _Returned; } set { OnReturnedChanging(value); ReportPropertyChanging("Returned"); _Returned = StructuralObject.SetValidValue(value); ReportPropertyChanged("Returned"); OnReturnedChanged(); } } private global::System.Boolean _Returned; partial void OnReturnedChanging(global::System.Boolean value); partial void OnReturnedChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 RentedBy { get { return _RentedBy; } set { OnRentedByChanging(value); ReportPropertyChanging("RentedBy"); _RentedBy = StructuralObject.SetValidValue(value); ReportPropertyChanged("RentedBy"); OnRentedByChanged(); } } private global::System.Int32 _RentedBy; partial void OnRentedByChanging(global::System.Int32 value); partial void OnRentedByChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.DateTime ReturnedOn { get { return _ReturnedOn; } set { OnReturnedOnChanging(value); ReportPropertyChanging("ReturnedOn"); _ReturnedOn = StructuralObject.SetValidValue(value); ReportPropertyChanged("ReturnedOn"); OnReturnedOnChanged(); } } private global::System.DateTime _ReturnedOn; partial void OnReturnedOnChanging(global::System.DateTime value); partial void OnReturnedOnChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Boolean OwnerReviewed { get { return _OwnerReviewed; } set { OnOwnerReviewedChanging(value); ReportPropertyChanging("OwnerReviewed"); _OwnerReviewed = StructuralObject.SetValidValue(value); ReportPropertyChanged("OwnerReviewed"); OnOwnerReviewedChanged(); } } private global::System.Boolean _OwnerReviewed; partial void OnOwnerReviewedChanging(global::System.Boolean value); partial void OnOwnerReviewedChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] public global::System.String RenterFeedback { get { return _RenterFeedback; } set { OnRenterFeedbackChanging(value); ReportPropertyChanging("RenterFeedback"); _RenterFeedback = StructuralObject.SetValidValue(value, true); ReportPropertyChanged("RenterFeedback"); OnRenterFeedbackChanged(); } } private global::System.String _RenterFeedback; partial void OnRenterFeedbackChanging(global::System.String value); partial void OnRenterFeedbackChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 BillingID { get { return _BillingID; } set { OnBillingIDChanging(value); ReportPropertyChanging("BillingID"); _BillingID = StructuralObject.SetValidValue(value); ReportPropertyChanged("BillingID"); OnBillingIDChanged(); } } private global::System.Int32 _BillingID; partial void OnBillingIDChanging(global::System.Int32 value); partial void OnBillingIDChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.Int32 ItemID { get { return _ItemID; } set { OnItemIDChanging(value); ReportPropertyChanging("ItemID"); _ItemID = StructuralObject.SetValidValue(value); ReportPropertyChanged("ItemID"); OnItemIDChanged(); } } private global::System.Int32 _ItemID; partial void OnItemIDChanging(global::System.Int32 value); partial void OnItemIDChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String CategoryDescription { get { return _CategoryDescription; } set { OnCategoryDescriptionChanging(value); ReportPropertyChanging("CategoryDescription"); _CategoryDescription = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("CategoryDescription"); OnCategoryDescriptionChanged(); } } private global::System.String _CategoryDescription; partial void OnCategoryDescriptionChanging(global::System.String value); partial void OnCategoryDescriptionChanged(); ///  /// No Metadata Documentation available. ///  [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] [DataMemberAttribute()] public global::System.String Title { get { return _Title; } set { OnTitleChanging(value); ReportPropertyChanging("Title"); _Title = StructuralObject.SetValidValue(value, false); ReportPropertyChanged("Title"); OnTitleChanged(); } } private global::System.String _Title; partial void OnTitleChanging(global::System.String value); partial void OnTitleChanged(); #endregion } 

任何帮助将不胜感激

我来到这里是因为我遇到了同样的问题,并希望得到答案。 但没有快乐! 所以我一直在寻找 – 在Pluralsight上有关于entity framework的培训video。 我找到了问题的答案。 我试图将DbContext与数据库生成的.edmx模型一起使用。 (不是代码优先)所以我需要将.edmx文件中的默认代码生成模板切换为Ado.NET DbContext Generator。 我也不得不下载这个因为它不在我看到的代码生成器列表中,当我右键单击.edmx设计器表面并选择“添加代码生成项”时从这里下载 : 下载用于C#的EF 4.x DbContext Generator或者这里是5.x – 看看这个链接。

所以在我将它作为.edmx文件的代码生成项安装之后,又向我的类中添加了一个构造函数,该构造函数派生自DbContext,它命名我的连接字符串

 public class ContentRepository: DbContext { public DbSet content_Templates { get; set; } public DbSet master_Templates { get; set; } public DbSet master_Content_Maps { get; set; } public ContentRepository() : base("name=MessagingSystemEntities") { } 

我的问题解决了。 也许它会帮助你,我希望如此。

上述就是C#学习教程:类型”未映射分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—编程笔记


推荐阅读
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了RxJava在Android开发中的广泛应用以及其在事件总线(Event Bus)实现中的使用方法。RxJava是一种基于观察者模式的异步java库,可以提高开发效率、降低维护成本。通过RxJava,开发者可以实现事件的异步处理和链式操作。对于已经具备RxJava基础的开发者来说,本文将详细介绍如何利用RxJava实现事件总线,并提供了使用建议。 ... [详细]
  • Todayatworksomeonetriedtoconvincemethat:今天在工作中有人试图说服我:{$obj->getTableInfo()}isfine ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 怀疑是每次都在新建文件,具体代码如下 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
author-avatar
然姐2502870593
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有