将IdentityDbContext存储在单独的数据库中,还原?

时间:2017-03-24 23:47:11

标签: c# identity dbcontext core

我对后端的AspNetIdentity有一些想法。

我使用SqlLite将IdentityDbContext存储为单独的数据库。

因此我有自己的dbcontext:" BaseDbContext",以及" IdentityDbContext"。

我自己的db-context在一个单独的数据库中注册,因为我不想被绑定到身份的结构。

但是,我有一个问题。如果我想将用户连接到其他表并跟踪操作等,我应该拥有自己的" tblUser"与身份一起,或者这将如何在实践中发挥作用?

所以基本上我的结构如下所示:

IdentityDb:

  • AspNetUsers
  • AspNetRoles
  • 等..

BaseDb:

  • tblProject
  • tblTask​​
  • tblUser ????

我以前在Identity中使用过自定义数据存储区,但重写和包装很多,并且我认为这次会采用传统设置。

对此有任何意见,我们非常感谢。 :)

谢谢!

2 个答案:

答案 0 :(得分:2)

  

它是如何引用IdentityDb的,因为它们是如此   不耦合?

Id表中的UserNameAspNetUsers值将在BaseDb的某些表格中引用。当你说"耦合"时,我假设你正在谈论一个外键来强制表之间的关系。您可以使用DataAnnotations或Fluent API创建外键(这是更好的,但不是必需的)。但基本上,只要您通过ASP.NET Identity获得UserIdUserName值,就可以使用该值从BaseDb中的某个表中获取/设置行。请参阅下面的代码。

  

您是指BaseDb中的列来表示这些Asp.Net值   没有实际的关系?

UserId数据库中的一个表(或多个表)中的UserNameBaseDb列可以引用UserIdUserName列中的AspNetUsers{ "ConnectionStrings": { "DefaultConnection": "Data Source=./IdentityDb.sqlite" "BaseConnection": "Data Source=./BaseDb.sqlite" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } } } 列。没有外键的public class Startup { // ... public IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services) { // ... services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); services.AddDbContext<BaseDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("BaseConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc(); services.AddScoped<ApplicationDbContext>(); services.AddScoped<BaseDbContext>(); // ... } // ... } 数据库可以强制执行该关系。但是,您可以使用DataAnnotations或Fluent API来创建外键。我没有为那个btw添加代码。

以下是一些我希望有用的示例代码:

<强> appsettings.json

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

Startup.cs代码段

public class BaseDbContext : DbContext
{
    public BaseDbContext(DbContextOptions<BaseDbContext> options)
        : base(options)
    {
    }

    public DbSet<Member> Members { get; set; }
}

<强> ApplicationDbContext.cs

public class Member
{
    public int Id { get; set; }

    public string UserName { get; set; } // to link with Identity

    // some other properties of Member
    public int Age { get; set; }
    public virtual ICollection<Favorite> Favorites { get; set; }
}

<强> BaseDbContext.cs

public class HomeController : Controller{
    private readonly ApplicationDbContext _identityContext;
    private readonly BaseDbContext _baseContext;

    public HomeController(ApplicationDbContext identityContext, BaseDbContext baseContext)
    {
        _identityContext = identityContext;
        _baseContext = baseContext;
    }

    public IActionResult Index()
    {
        // get currently logged in user's username from ASP.NET Identity
        string userName = User.Identity.Name;

        // get username in Member table of baseDb where Id column is 123
        string anotherUserName = _context.Member().FirstOrDefault(m => m.Id == 123).UserName;

        // add new row in basedb.Member table with identity username as foreign key
        var model = new Member
        {
            UserName = userName,
            Age = 1,
            Favorites = new List<Favorite>();
        };
        _baseContext.Add(model);
        _baseContext.SaveChanges();

        return View();
    }
}

<强> Member.cs

2.51

然后在任何一个Controller类中,从ASP.NET Identity获取当前登录用户的用户名,在baseDb的Member表中获取用户名,在baseDb的Member表中保存用户名等等。

HomeController.cs片段

1.13.1

答案 1 :(得分:0)

我认为您不需要在其他数据库(BaseDb)中创建用户表。如果要在IdentityDb数据库中引用ASP.NET身份创建的用户,只需要在IdentityDb数据库中的AspNetUsers表中引用Id或UserName列作为BaseDb数据库中所需的外键。

相关问题