I'm developing a web application using ASP.NET MVC5 and ASP.NET Identity to user authentication. My data base is built with EF Code First. I have extended ApplicationUser with some properties:
public class ApplicationUser : IdentityUser
{
[StringLength(80)]
[Display(Name = "FullName", ResourceType = typeof(Resources.Resources))]
public string FullName { get; set; }
// More properties
public virtual ICollection<Installation> Installations { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
This is Installation class (as you can see the Key attribute is defined):
public class Installation
{
[Key]
[Required]
[StringLength(15)]
[Display(Name = "Imei", ResourceType = typeof(Resources.Resources))]
public string Imei { get; set; }
[Display(Name = "Name", ResourceType = typeof(Resources.Resources))]
[StringLength(50)]
[Required]
public string Name { get; set; }
// More properties
public virtual ICollection<ApplicationUser> Users { get; set; }
}
These classes are defined in a class library. This library is referenced in the web application project and the Windows Forms project.
Following this answer I use this two lines to login with user and password:
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
ApplicationUser user = await userManager.FindAsync(this.tbUser.Text, this.tbPassword.Text);
The web application runs well, I can create, edit, delete and view details of users and installations depending on the role of the user logged in. But I have problems to get the user in the Windows Forms application. I get this excetion in the user.Manager.FindAsync() line:
System.Data.Entity.ModelConfiguration.ModelValidationException was unhandled by user code
HResult=-2146233088
Message=One or more validation errors were detected during model generation:
MyProjectWeb.Models.Installation: : EntityType 'Installation' has no key defined. Define the key for this EntityType.
Installations: EntityType: EntitySet 'Installations' is based on type 'Installation' that has no keys defined.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at System.Data.Entity.QueryableExtensions.Include[T](IQueryable`1 source, String path)
at System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable`1 source, Expression`1 path)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.GetUserAggregateAsync(Expression`1 filter)
at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.FindByNameAsync(String userName)
at Microsoft.AspNet.Identity.UserManager`2.FindByNameAsync(String userName)
at Microsoft.AspNet.Identity.UserManager`2.<FindAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyProjectForm.FormConectar.<Login>d__2.MoveNext() in f:\...\Solution\MyProjectForm\FormConectar.cs:line 158
InnerException:
There is no InnerException information. I tried this to get more information but these are different exceptions so it never go into the catch block.
The strangest thing is that before it did work well, but I don't know what I did and now I get this error.
Aucun commentaire:
Enregistrer un commentaire