0
votes

I have a problem with Asp.Net when I'm trying to create a many to many relationship model, I get this error when trying to register an account :

Sequence contains no matching element

When I "disabled" my changes, I could register on the site normally so there are some errors in my models regarding the column structure I suppose but I'm unable to fix them myself. I want to create a many to many connection between my Bookmark and Tag tables and the many-many table should have following structure: Id, BookmarkID, TagID.

Here are my models:

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

Here is the error stack trace as well:

[InvalidOperationException: Sequence contains no matching element]
System.Linq.Enumerable.Single(IEnumerable1 source, Func2 predicate) +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 table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration) +138
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.<>c__DisplayClass4.b__3(Tuple2 pm) +38
System.Data.Entity.Utilities.IEnumerableExtensions.Each(IEnumerable
1 ts, Action1 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(IList1 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 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.RetryLazy2.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.InternalSet1.get_InternalContext() +16 System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() +39 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable1 source, Expression1 predicate, CancellationToken cancellationToken) +154 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable1 source, Expression1 predicate) +163
Microsoft.AspNet.Identity.EntityFramework.d__6c.MoveNext() +472 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 Microsoft.AspNet.Identity.CultureAwaiter1.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() in D:\Projekti\CS\BookIT\BookIT\Controllers\AccountController.cs:155
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +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.WrappedAsyncResult1.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.WrappedAsyncResult1.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.WrappedAsyncResult1.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.WrappedAsyncVoid1.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 controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid1.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.WrappedAsyncVoid1.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 result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9711525 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Thanks in advance

Added:

  if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
2
Have you checked the InnerException message?CodeNotFound
BTW you don't need BookmarkTagId in the composite keys in BookmarkTag. Just BookmarkId and TagId are sufficient.CodeNotFound
Can you add the code that is actually throwing the error with these models? Your stack trace indicates that it's around AccountController:155. Based upon the exception text it looks like you're calling .Single() or .First() but we cannot know that without looking at the actual code.cmcquillan
Yes it's throwing the error at that exact line but i haven't changed anything in the code besides my own models that i put up there and when i remove those models the registration goes through... The rest of the code is the defualt when you create a new mvc program and check individual accounts option @CodeNotFound yeah i know i just put it there cause i want to add an extra columnTokikko
@IvanRepusic then what is the predicate you give to your Single() or First() method?CodeNotFound

2 Answers

0
votes

In the end i found the reason that caused the error... IT was this line

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

Whene i removed the Column(TypeName) part the code went to compile without any problems... It works now but i'm not sure why it does or why did it throw that error. Can anyone explain why did it not work before?

0
votes

You have too many Key attributes, the EF Framework cannot work out your model. You can use fluent api to configure exactly what you want. The easiest way you can solve your issue is to use EF feature Convention Over Configuration. Basically, follow the conventions and the EF Framework will do the heavy lifting for you. I have added here a convention over configuration approach and it runs in at least in my visual studio. I have tested this and it runs successfully.

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