2012-11-17 199 views
4

我一直在嘗試修復此錯誤一段時間。類型''未映射

我的應用程序嘗試創建我的數據上下文的實例時,出現此錯誤。

下面是代碼:

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<Rental> Rentals { get; set; } 
      public DbSet<Category> Categories { get; set; } 
      public DbSet<Item> Items { get; set; } 
      public DbSet<Billing> Billings { get; set; } 
      public DbSet<User> Users { get; set; } 
      public DbSet<Video> Videos { get; set; } 
      public DbSet<Picture> Pictures { get; set; } 
      public DbSet<ServiceType> ServiceTypes { get; set; } 
      public DbSet<Rating> Ratings { get; set; } 
      public DbSet<Business> Businesses { get; set; } 
      public DbSet<BusinessHour> BusinessHours { get; set; } 
     } 
    } 

這裏是堆棧跟蹤:

[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:\Users\nikka\Desktop\RandomRentals\RandomRentals\Models\UserModel.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.<BeginProcessRequest>b__5() +37 
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12 
    System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 
    System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50 
    System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>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數據註釋檢查類型是否未被明確排除。驗證類型是否定義爲一個類,不是原始的,嵌套的或通用的,並且不從EntityObject繼承。

編輯:租賃類的代碼。

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

公共部分類服務:EntityObject { #地區的工廠方法

/// <summary> 
/// Create a new Rental object. 
/// </summary> 
/// <param name="rentalID">Initial value of the RentalID property.</param> 
/// <param name="agreedPrice">Initial value of the AgreedPrice property.</param> 
/// <param name="rentedOn">Initial value of the RentedOn property.</param> 
/// <param name="rentalDays">Initial value of the RentalDays property.</param> 
/// <param name="returned">Initial value of the Returned property.</param> 
/// <param name="rentedBy">Initial value of the RentedBy property.</param> 
/// <param name="returnedOn">Initial value of the ReturnedOn property.</param> 
/// <param name="ownerReviewed">Initial value of the OwnerReviewed property.</param> 
/// <param name="billingID">Initial value of the BillingID property.</param> 
/// <param name="itemID">Initial value of the ItemID property.</param> 
/// <param name="categoryDescription">Initial value of the CategoryDescription property.</param> 
/// <param name="title">Initial value of the Title property.</param> 
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 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
[DataMemberAttribute()] 
public Nullable<global::System.Decimal> ProposedPrice 
{ 
    get 
    { 
     return _ProposedPrice; 
    } 
    set 
    { 
     OnProposedPriceChanging(value); 
     ReportPropertyChanging("ProposedPrice"); 
     _ProposedPrice = StructuralObject.SetValidValue(value); 
     ReportPropertyChanged("ProposedPrice"); 
     OnProposedPriceChanged(); 
    } 
} 
private Nullable<global::System.Decimal> _ProposedPrice; 
partial void OnProposedPriceChanging(Nullable<global::System.Decimal> value); 
partial void OnProposedPriceChanged(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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(); 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[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 

} 

任何幫助,將不勝感激

+0

我想你應該發佈ex.ToString()結果。我需要包含在例外中的完整堆棧跟蹤。 – Rikki

+0

是的,我忘記補充說,我發佈之前。剛剛添加。 – Mike

+0

我認爲這個問題來自RandomRentals.Rental類。你可以發佈Rental類的代碼嗎? – Mark

回答

10

我來到這裏是因爲我有同樣的問題,並且是希望得到答案。但沒有快樂!所以我一直在尋找 - 在Pluralsight上有關於Entity Framework的培訓視頻。我找到了我的問題的答案。我試圖使用DbContext和數據庫生成的.edmx模型。 (不代碼優先)因此,我需要將.edmx文件中的默認代碼生成模板轉換爲Ado.NET DbContext生成器。我也不得不降負荷,是因爲它在我看到的時候我右邊點擊的.edmx設計器表面上,從這裏選擇「添加代碼生成項目」下載的代碼生成清單並非:download EF 4.x DbContext Generator for C# 或這裏for 5.x - check out this link.

所以,當我安裝了此作爲.edmx文件代碼生成項目,並增加了一個構造上我的課,從其中的DbContext我的名字連接字符串

public class ContentRepository: DbContext 
{ 
    public DbSet<Content_Template> content_Templates { get; set; } 
    public DbSet<Master_Template> master_Templates { get; set; } 
    public DbSet<Master_Content_Map> master_Content_Maps { get; set; } 

    public ContentRepository() 
     : base("name=MessagingSystemEntities") 
    { 
    } 

我的問題就解決了派生。也許它也會幫助你,我希望如此。

+1

Omg我愛你。 – Mike

+1

:-)我喜歡Julie Lehrman,他製作了多重視頻幫助我。 – bperreault

+0

@Mike如果解決了您的問題,您應該考慮將此答案標記爲正確。 :) – Termato