使用多个上下文应用自定义ASPNET身份一对多的关系 [英] Custom ASPNET Identity one to many relationship using multiple context application

查看:142
本文介绍了使用多个上下文应用自定义ASPNET身份一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想有一个可以创造自己的故事的用户。

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

我有这些类:

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

然后做迁移我的故事使用的contenxt实体 MyDbContext 失败说。

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.

但是当我尝试在身边,我会用做迁移的其他方式的 IdentityContext 这将创建一个新表故事

现在,什么工作是我的合并环境。喜欢的东西。

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?

推荐答案

您不能从一个上下文引用实体在另一个,或者这方面将试图管理这些实体为好,导致大约已有的表的错误。你有两个选择:

You can't reference entities from one context in another, or that context will attempt to manage those entities as well, resulting in errors about tables already existing. You have two options:


  1. 如果你不实际的需求的两个不同的上下文(即它们都是code首先,你是罚款一切都在一个数据库中),那么最好和最简单的解决方法就是将它们合并为你做。还有具有多种环境没有好处,正如你所看到的,有很多不利的。永远使用多个上下文唯一的好理由是,如果你处理的附加现有的数据库。

  1. If you don't actually need two separate contexts (i.e., they're both Code First and you're fine with everything being in one database), then the best and easiest solution is to just merge them as you've done. There's no benefit to having multiple contexts, and as you've seen, there's plenty of detriment. The only good reason to ever use multiple contexts is if you're dealing with additional existing databases.

创建一个简单的列来存储相关的ID(不是外键)。你失去了其真正的外键并且能够延迟加载的优化,但你仍然可以至少有点关系的东西这种方式。从本质上讲,你只需设置该属性与其他上下文关联对象的ID。然后,当你需要检索对象,您只执行与其他方面的查询,利用该id。换句话说,你只需手动获取的对象。

Create a simple column to store the related id (not a foreign key). You lose the optimization of having a true foreign key and the ability to lazy load, but you can still at least somewhat relate things this way. Essentially, you just set this property with the id of the related object in the other context. Then, when you need to retrieve that object, you just issue a query with that other context, utilizing that id. In other words, you just manually fetch the objects.

这是你唯一的选择,很遗憾。

That's your only options, unfortunately.

这篇关于使用多个上下文应用自定义ASPNET身份一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆