EF:关于跨数据库关系数据库设计问题 [英] EF: Database design issues regarding cross database relations

查看:207
本文介绍了EF:关于跨数据库关系数据库设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前原型设计(非常直接的?)多租户网络应用程序,用户(存储在数据库中的 1 )可以注册不同的租户(存储在每个租户(同DB模式的数据库)。我想到了一个架构将适用于大量的多租户解决方案。

I am currently prototyping a (very straight-forward?) multi-tenant web-application where users (stored in database 1) can register to different tenants (stored in a database per tenant (same db schema). An architecture that I thought would apply to a lot of multi tenant solutions.

可悲的是,我发现,跨数据库关系不是在实体框架的支持(我认为它仍然是EF6的情况下)。我下面提供的链接。

Sadly, I found out that cross database relations are not supported in Entity Framework (I assumed it's still the case for EF6). I provided the links below.

接下来的小段解释我的问题,最终我的问题(S)。

The next short sections explain my problem, and ultimately my question(s).

我选择有不同的数据库,一个是用户(1),一个是与他们的客户信息每个租户这样,用户不必当他加入另一个租户创建一个新帐户(一个客户。可以有不同的部门有不同的域)。

I choose to have separate databases; one for users (1), and one for each tenant with their customer specific information. That way a user does not have to create a new account when he joins another tenant (one customer can have different domains for different departments).

我实现了这个使用两种不同的的DbContext S,一个是用户,一个租户的信息。在持有它指的是用户实体 TenantContext 我定义 DbSet 取值实体(导航属性)

I implemented this using two different DbContexts, one for the users, and one for the tenant information. In the TenantContext I define DbSets which holds entities which refer to the User entity (navigation properties).

在'每个租户的语境:

public class CaseApplicationContext : DbContext, IDbContext
{
    public DbSet<CaseType> CaseTypes { get; set; } 
    public DbSet<Case> Cases { get; set; }

    // left out some irrelevant code
}



案例实体:

[Table("Cases")]
public class Case : IEntity
{
    public int Id { get; set; }
    public User Owner { get; set; } // <== the navigation property
    public string Title { get; set; }
    public string Description { get; set; }

    public Case()
    {
        Tasks = new List<Task>();
    }
}



用户实体

[Table("Users")]
public class User : IEntity
{
    public int Id { get; set; }

    public string Name { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
}

用户实体也由用户数据库通过我的其他的DbContext衍生包含:

This User entity is also contained by the Users database by my other DbContext derivative:

public class TenantApplicationContext : DbContext, IDbContext
{
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<User> Users { get; set; } // <== here it is again

    // left out irrelevant code
}



现在,什么不顺心



预计:

我(在我所有的愚蠢)的认为会发生的是,我实际上建立一个跨数据库关系:

What I (in all my stupidity) thought that would happen is that I would actually create a cross database relation:

每个租户的数据库包含表'案例'。此表包含了用户名行。该用户名指的是用户数据库

The 'per-tenant' database contains a table 'Cases'. This table contains rows with a 'UserID'. The 'UserID' refers to the 'Users' database.

实际

当我开始添加案例取值我也创建的其他的表'用户'我'每个租户的数据库。在我的'情况'表中的用户名是指在同一个数据库中的表。

When I start adding Cases I am also creating another table 'Users' in my 'per-tenant' database. In my 'cases' table the UserID refers to the table in the same database.

所以我开始google搜索,发现这个功能根本不支持。这使我想到,我居然用EF为这样的应用程序?我应该对NHibernate的移动呢?

So I started googling, and found that this feature simply is not supported. This made me think, should I even use EF for an application like this? Should I move towards NHibernate instead?

但我也无法想象,在庞大市场对多租户应用程序只是由微软的实体框架忽视?所以,我最有可能在做一些很愚蠢。

But I also can't imagine that the huge market for multi tenant applications simply is ignored by Microsoft's Entity Framework?! So I most probably am doing something rather stupid.

我认为主要的问题是关于我的数据库设计。由于我是新来的EF和学习,我走了,我可能会采取多次转错了(是我设计的坏了?)。既然有那么很好与EF专家代表,我非常渴望学习,我可以使用的替代品来达到同样的事情(多租户共享的用户,在蔚蓝的部署)。我应该使用一个单一的的DbContext ,仍然能够部署多租户web应用程序与共享用户数据库?

I think the main question is about my 'database design'. Since I am new to EF and learning as I go, I might have taken the wrong turn on several occasions (is my design broken?). Since SO is well represented with EF experts I am very eager to learn which alternatives I could use to achieve the same thing (multi tenant, shared users, deployable in azure). Should I use one single DbContext and still be able to deploy a multi tenant web-application with a shared Users database?

我会很感激你的帮助!

物联网据悉:


  • NHibernate的不支持跨数据库的关系(但我要部署到Azure和而坚持微软技术)

  • 的意见或Synomyms可替代(不知道是否会在Azure中造成更多的困难)

  • 跨数据库的关系不是由EF支持:
    • NHibernate does support cross database relations (but I want to deploy into Azure and rather stick to microsoft technologies)
    • Views or Synomyms can be an alternative (not sure if that will cause more difficulties in Azure)
    • Cross database relations are not supported by EF:

      • PS:我意识到这是一个冗长的问题。随意编辑的问题,并删除无关的部分,以提高可读性。

      • PPS:如果需要,我可以分享更多的代码

      感谢你这么多提前。我会很乐意回报你upvotes的所有努力!

      Thank you so much in advance. I will gladly reward you with upvotes for all your efforts!

      推荐答案

      我不明白你为什么需要跨数据库的关系在所有。假设你的应用程序可以倾诉的两个数据库,用户数据库和租户数据库,它可以很容易使用的第一个数据库进行认证,然后找到与租户数据库相关的用户按名称约定。

      I don't quite understand why do you need cross database relations at all. Assuming your application can talk to the two databases, the user database and a tenant database, it can easily use the first database for authentication and then find related user in the tenant database with "by name" convention.

      例如,如果您使用的用户数据库,那么你搜索的租户数据库用户JOHN验证用户JOHN。

      For example, if you authenticate a user JOHN using user database then you search for a user JOHN in the tenant database.

      这是实施更容易,还是满足您的要求,用户在使用自己的密码和用户记录卷影副本,但没有密码存储在用户数据库一起存储在租户数据库和在这两个之间没有任何物理关系

      This would be much easier to implement and still match your requirements, users are stored in users database together with their passwords and "shadow copies" of user records but with no passwords are stored in tenant databases and there is NO physical relation between these two.

      这篇关于EF:关于跨数据库关系数据库设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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