繁体   English   中英

尝试在asp.net中创建多对多关系时出错

[英]Error when trying to create many to many relationship in asp.net

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

序列不包含匹配元素

当我“禁用”我的更改时,我可以正常在该站点上注册,因此我的模型中存在一些与我假设的列结构有关的错误,但我自己无法修复。 我想在我的BookmarkTag表之间创建多对多连接,并且多对多表应具有以下结构: IdBookmarkIDTagID

这是我的模型:

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;}
}

这也是错误堆栈跟踪:

[InvalidOperationException:序列不包含匹配的元素]
System.Linq.Enumerable.Single(IEnumerable 1 source, Func 2谓词)+2603017 System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest,字符串名称)+81
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty列,EntityType表,DbProviderManifest providerManifest)+87
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty列,EntityType表,DbProviderManifest providerManifest,布尔值allowOverride,布尔值fillFromExistingConfiguration)+138
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive。<> c__DisplayClass4.b__3(Tuple 2 pm) +38
System.Data.Entity.Utilities.IEnumerableExtensions.Each(IEnumerable
2 pm) +38
System.Data.Entity.Utilities.IEnumerableExtensions.Each(IEnumerable
2 pm) +38
System.Data.Entity.Utilities.IEnumerableExtensions.Each(IEnumerable
1 ts,动作1 action) +130
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable
1 action) +130
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable
1 action) +130
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable
1 propertyMappings,DbProviderManifest providerManifest,Boolean allowOverride,Boolean fillFromExistingConfiguration)+94

System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList 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 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 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实体集,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 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
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
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.Initialize()+53

System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext() +16 System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider 1.get_InternalContext() +16 System.Data.Entity.Infrastructure.DbQuery 39 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync( IQueryable 1 source, Expression 1谓词,CancellationToken cancelToken)+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.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.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.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.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。<> c__DisplayClass37.b__36(IAsyncResult asyncResult)+17
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49

System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d()+50 System.Web.Mvc.Async。<> c__DisplayClass46.b__3f()+225 System.Web.Mvc.Async。<> c__DisplayClass33.b__32(IAsyncResult asyncResult)+ 10
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
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。<> c__DisplayClass2b.b__1c()+26 System.Web.Mvc.Async。<> c__DisplayClass21.b__1e(IAsyncResult asyncResult)+100
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
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) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49

System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+36 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult,Controller控制器)+12
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +22
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) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.CallEndDelegate(IAsyncResult asyncResult) +29
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步骤,布尔值且已完成)+155

提前致谢

添加:

  if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {

您的Key属性过多,EF框架无法计算出您的模型。 您可以使用流利的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; }
 }

最后,我找到了导致错误的原因...它就是这一行

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

当我删除Column(TypeName)部分时,代码可以毫无问题地进行编译...现在可以正常工作,但是我不确定为什么这样做或为什么抛出该错误。 谁能解释为什么以前不起作用?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM