简体   繁体   English

实体框架代码首先使用SQL Server同义词

[英]Entity Framework Code First with SQL Server Synonyms

I have the situation where I will have multiple companies accessing a single instance of my application, but I need to segregate their data to avoid accidentally loading data for another company, and to ease per-company database backups. 我有这样的情况,我将有多个公司访问我的应用程序的单个实例,但我需要隔离他们的数据,以避免意外地为另一家公司加载数据,并简化每个公司的数据库备份。

Ideally, I'd like to split the data up into different databases as follows: 理想情况下,我想将数据拆分到不同的数据库中,如下所示:

  • One SQL Server database for a list of all the users, together with any tables that are common across all users / companies 一个SQL Server数据库,包含所有用户的列表,以及所有用户/公司通用的任何表

  • N number of company specific databases , each with the same schema but different data for each company. N个公司特定的数据库 ,每个数据库具有相同的模式,但每个公司的数据不同。 On logging in, I would dynamically select the database for the specific user's company 登录时,我会动态选择特定用户公司的数据库

I'd like to be able to query the company specific data tables, but perform joins onto the shared database (for example, so I could store a foreign key in the company database table, which joins onto the primary key of a shared table in the other database). 我希望能够查询公司特定的数据表,但是可以在共享数据库上执行连接(例如,我可以将外键存储在公司数据库表中,该表连接到共享表的主键上另一个数据库)。

I've discovered SQL Server Synonyms which look like they could do the job, and it seems I can successfully join between the two databases by using a query in SQL Server Management Studio. 我发现了SQL Server同义词,看起来他们可以完成这项工作,而且我似乎可以通过在SQL Server Management Studio中使用查询在两个数据库之间成功连接。

Is it possible to use a Synonym table in Entity Framework Code First, and if so what would my POCO classes / DbContext need to look like? 是否可以在Entity Framework Code First中使用同义词表,如果是这样,我的POCO类/ DbContext需要看起来像什么?

Thanks :) 谢谢 :)

If I understood properly, you have a SharedServer and some LocalServers (company specific) which want to have all the objects of both (one shared, one the company specific) in a single context. 如果我理解得当,你有一个SharedServer和一些LocalServers (特定于公司),它们希望在一个上下文中拥有两者的所有对象(一个共享,一个公司特定)。

I'll give you two scenarios: 我会给你两个场景:

  1. Many-to-Many : in this case, table to have relation are in sharedDB , but the third table joining them, is in company specific DB . 多对多 :在这种情况下,具有关系的表位于sharedDB中 ,但加入它们的第三个表位于公司特定的DB中
  2. Single-to-Many : which one of tables are in SharedDB and the other one in company specific DB . 单对多 :其中一个表位于SharedDB中 ,另一个表位于公司特定的DB中

Many-to-Many 许多一对多

1. Create Your Synonym in SQL side 1.在SQL端创建同义词

First you have to create the synonym in your local (or company specific) DB: 首先,您必须在本地(或公司特定)数据库中创建同义词:

CREATE SYNONYM [dbo].[StudentCources] FOR [SharedServer].[SharedDB].[dbo].[StudentCources]

let's suppose that your shared table has two columns (doesn't care) named studentID and courseID . 我们假设您的共享表有两列(无关)名为studentIDcourseID

2. Create the POCOs 2.创建POCO

Let's suppose we have two tables on local DB which have Many-to-Many relationship between each other. 假设我们在本地数据库上有两个表,它们之间具有多对多关系。 and let's suppose the third joiner table (which contains the keys) is located in shared DB!! 让我们假设第三个连接表(包含键)位于共享DB中! (I think it's the worst way). (我认为这是最糟糕的方式)。 so your POCOs will look like this: 所以你的POCO看起来像这样:

Public Class Student
    Public Property studentID as Integer
    Public Property Name as String
    Public Property Courses as ICollection(Of Course)
End Class

and

Public Class Course
    Public Property courseID as Integer
    Public Property Name as String
    Public Property Students as ICollection(Of Student)
End Class

and the Shared one: 共享的

Public Class StudentCources
    Public Property courseID as Integer
    Public Property studentID as Integer
End Class

and the context look like: 上下文如下:

Partial Public Class LocalContext
    Inherits DbContext

    Public Sub New()
        MyBase.New("name=LocalContext")        
    End Sub

    Public Overridable Property Students As DbSet(Of Student)
    Public Overridable Property Courses As DbSet(Of Course)

    Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
        modelBuilder.Entity(Of Student).HasMany(Function(e) e.Courses).WithMany(Function(e) e.Students).Map(Sub(e)
            e.MapLeftKey("studentID")
            e.MapRightKey("courseID")
            e.ToTable("StudentCources", "dbo")
    End Sub)

    End Sub
End Class

the code in the OnModelCreating tells the model builder that the relation table is a synonym (not directly). OnModelCreating的代码告诉模型构建器关系表是同义词(不是直接)。 and we know that the synonym is in SharedDB . 我们知道同义词在SharedDB中

One-to-Many 一个一对多

No steps! 没有步骤! Just modify the OnModelCreating to: 只需将OnModelCreating修改为:

modelBuilder.Entity(Of Student).ToTable("Students", "dbo")

and note that in this case Students is a Synonym . 并注意在这种情况下, Students同义词 then create the relationship :) 然后创建关系:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM