Custom ASPNET Identity one to many relationship using multiple context application

Viewed 1276

Basically, I want to have a user that can create their own stories.

I have these classes:

public class ApplicationUser : IdentityUser
{
  public string DisplayedName { get; set; }
}

public class Story
{
  public int Id { get; set; }
  public string Content { get; set; }
}

They are managed on a different context and so as their migration. Something like this.

public class MyDbContext : DbContext
{
  public DbSet<Story> Stories { get; set; }
}

public class IdentityContext : IdentityDbContext<ApplicationUser>
{
}

When I try to add a migration then update them individually, it works fine but when I try to add a collection of stories in my application user.

public class ApplicationUser : IdentityUser
{
  public string DisplayedName { get; set; }
  public virtual ICollection<Story> Stories { get; set; }
}

public class Story
{
  public int Id { get; set; }
  public string Content { get; set; }
  public string WrittenById { get; set; }
  public virtual ApplicationUser WrittenBy { get; set; }
}

public class StoryMap : EntityTypeConfiguration<Story>
{
  public StoryMap()
  {
    HasOptional(s => s.WrittenBy)
      .WithMany(s => s.Stories)
      .HasForeignKey(s => s.WrittenById)
      .WillCascadeOnDelete(false);
  }
}

Then do a migration on my Story entity using the contenxt of MyDbContext it fails saying.

Data.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Data.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

But when I try the other way around in which I'll do a migration using the IdentityContext it would create a new table of Story

For now, what works is merging my contexts. Something like.

public class MyDbContext : IdentityDbContext<ApplicationUser>
{
  public DbSet<Story> Stories { get; set; }
}

But there must be a way of managing them separately, right? Or am I doing it all wrong?

1 Answers
Related