尝试在asp.net

时间:2015-11-22 14:05:59

标签: c# asp.net-mvc entity-framework

当我尝试创建多对多关系模型时,Asp.Net出现问题,尝试注册帐户时出现此错误:

  

序列不包含匹配元素

当我"禁用"我的更改,我可以正常在网站上注册,所以我的模型中有关于列结构的一些错误,我想但我自己无法修复它们。我想在我的BookmarkTag表之间创建多对多的连接,并且许多表应具有以下结构:IdBookmarkID,{{1} }。

以下是我的模特:

TagID

以下是错误堆栈跟踪:

  

[InvalidOperationException:Sequence不包含匹配元素]
  System.Linq.Enumerable.Single(IEnumerable public class Bookmark { [Key] public int BookmarkID { get; set; } public string Url { get; set; } public ApplicationUser UserId { get; set; } public virtual ICollection<BookmarkTag> BookmarkTags { get; set; } } public class Tag { [Key] public int TagID { get; set; } [Column(TypeName="varchar(60)")] public string TagName { get; set; } public virtual ICollection<BookmarkTag> BookmarkTags { get; set; } } public class BookmarkTag { [Key, Column(Order = 0)] public int BookmarkTagID { get; set; } [Key, Column(Order = 1)] public int BookmarkID { get; set; } [Key, Column(Order = 2)] public int TagID { get; set; } public virtual Bookmark Bookmark { get; set; } public virtual Tag Tag { get; set;} } 2谓词)   +2603017 System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest)   providerManifest,String name)+81
  System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty   column,EntityType table,DbProviderManifest providerManifest)+87
  System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty   column,EntityType表,DbProviderManifest providerManifest,Boolean   allowOverride,Boolean fillFromExistingConfiguration)+138
  System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive&LT;&GT; c__DisplayClass4.b__3(元组1 source, Func 1   行动2 pm) +38
System.Data.Entity.Utilities.IEnumerableExtensions.Each(IEnumerable
1   propertyMappings,DbProviderManifest providerManifest,Boolean   allowOverride,Boolean fillFromExistingConfiguration)+94
  System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList 1 action) +130
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable
1 entitySets,DbProviderManifest   providerManifest)+423
  System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping   databaseMapping,DbProviderManifest providerManifest)+376
  System.Data.Entity.DbModelBuilder.Build(DbProviderManifest   providerManifest,DbProviderInfo providerInfo)+444
  System.Data.Entity.DbModelBuilder.Build(的DbConnection   providerConnection)+55
  System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext   internalContext)+61
  System.Data.Entity.Internal.RetryLazy 1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride) +154
System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride) +585
System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) +177
System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection
1.Initialize()+53
  System.Data.Entity.Internal.Linq.InternalSet 2.GetValue(TInput input) +123
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +616 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
System.Data.Entity.Internal.Linq.InternalSet
1.System.Linq.IQueryable.get_Provider()   +39 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable 1.get_InternalContext() +16 System.Data.Entity.Infrastructure.DbQuery 1个谓词,CancellationToken cancellationToken)   +154 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable 1 source, Expression 1谓词)+163
  Microsoft.AspNet.Identity.EntityFramework.d__6c.MoveNext()   +472 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+99
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58 Microsoft.AspNet.Identity.CultureAwaiter 1 source, Expression 1.GetResult()   +28 BookIT.Controllers.d__15.MoveNext()在D:\ Projekti \ CS \ BookIT \ BookIT \ Controllers \ AccountController.cs:155
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+99
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58
  System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult的   asyncResult)+97
  System.Web.Mvc.Async&LT;&GT; c__DisplayClass37.b__36(IAsyncResult的   asyncResult)+17
  System.Web.Mvc.Async.WrappedAsyncResult 1.GetResult() +59 Microsoft.AspNet.Identity.<ValidateUserName>d__4.MoveNext() +594 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 Microsoft.AspNet.Identity.<ValidateAsync>d__0.MoveNext() +266 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +568 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 Microsoft.AspNet.Identity.<CreateAsync>d__d.MoveNext() +483 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter
1.End()+49
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult的   asyncResult)+32
  System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d()   +50 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass46.b__3f()   +225 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass33.b__32(IAsyncResult asyncResult)+10
  System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult的   asyncResult)+34
  System.Web.Mvc.Async&LT;&GT; c__DisplayClass2b.b__1c()   +26 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21.b__1e(IAsyncResult)   asyncResult)+100
  System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult的   asyncResult)+27
  System.Web.Mvc.Controller.b__1d(IAsyncResult的   asyncResult,ExecuteCoreState innerState)+13
  System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49
  System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+36   System.Web.Mvc.Controller.b__15(IAsyncResult的   asyncResult,控制器控制器)+12
  System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49
  System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+26
  System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult的   asyncResult)+10
  System.Web.Mvc.MvcHandler.b__5(IAsyncResult的   asyncResult,ProcessRequestState innerState)+21
  System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)   +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult)   结果)+9
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +9711525 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155

提前致谢

添加了:

1.CallEndDelegate(IAsyncResult
  asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase

2 个答案:

答案 0 :(得分:0)

您有太多Key属性,EF Framework无法解决您的模型。您可以使用流畅的api来准确配置您想要的内容。解决问题的最简单方法是使用EF功能约定优于配置。基本上,遵循惯例,EF框架将为您做繁重的工作。我在这里添加了一个关于配置方法的约定,它至少在我的visual studio中运行。我测试了这个,它运行成功。

public class Bookmark
{
   public int BookmarkID { get; set; }
   public string Url { get; set; }
   public ApplicationUser UserId { get; set; }
   public virtual ICollection<BookmarkTag> BookmarkTags { get; set; }
 }

 public class BookmarkTag
 {
    public int BookmarkTagID { get; set; }
    public virtual Bookmark Bookmark { get; set; }
    public virtual Tag Tag { get; set; }
 }

 public class Tag
 {
    public int TagID { get; set; }
    public string TagName { get; set; }
    public virtual ICollection<BookmarkTag> BookmarkTags { get; set; }
 }

答案 1 :(得分:0)

最后我发现导致错误的原因...... 这就是这条线

 [Column(TypeName="varchar(60)")]
public string TagName { get; set; }

Whene我删除了Column(TypeName)部分代码进行编译没有任何问题......它现在有效但我不确定它为什么会这样做或为什么它会抛出该错误。任何人都可以解释为什么它以前不起作用?