public class MyActivity
{
public Guid Id { get; set; }
public ApplicationUser User { get; set; }
public string SomeProp { get; set; }
}
: Ben MyActivity sınıfını tanımlanan
public class ApplicationUser : IdentityUser
{
public ICollection<MyActivity> Activities { get; set; } // Added that
public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
return Task.FromResult(GenerateUserIdentity(manager));
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Sonra "add-migrasyon UserActivities" ve "update-database" i çalıştırdım. AspNetUsers.Id başvuruyor, bir User_Id yabancı anahtar ile oluşturulan bir Etki Alanı tablosu var. Her şey yolunda. Ben bir aktiviteyi eklemeye çalıştığınızda
Ama şimdi,:
using (var db = new ApplicationDbContext())
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var currentUser = manager.FindById(User.Identity.GetUserId());
currentUser.Activities.Add(new MyActivity { SomeProp = "1" });
db.SaveChanges();
}
Ben NullReferenceException
(currentUser.Activities
boş) olsun.
Sorun nedir?