简体   繁体   English

EF4 / 5自动生成LocalDb数据库:SQL Server等效项是什么?

[英]EF4/5 autogenerates LocalDb databases: what's the SQL Server equivalent?

I've been developing a web application that uses Entity Framework 5 as the ORM. 我一直在开发使用Entity Framework 5作为ORM的Web应用程序。 EF5 will auto-generate a file for each new database I want (with its default behavior with no connection string but System.Data.Entity.Infrastructure.LocalDbConnectionFactory specified in the web.config). EF5将为我想要的每个新数据库自动生成一个文件(其默认行为是没有连接字符串,但在web.config中指定了System.Data.Entity.Infrastructure.LocalDbConnectionFactory)。 I've been generating a new database for each customer. 我一直在为每个客户生成一个新的数据库。 I expect no more than 5000 total customers in my lifetime. 我希望一生中不超过5000个客户。 How do I translate this functionality onto the big Sql Server? 如何将此功能转换到大型Sql Server上? Do I need to write my own code to create databases on there? 我需要编写自己的代码以在此处创建数据库吗? Where would that go? 那会去哪里?

At my workplace we generate and store a create script for each successive version of our application so that at any time we can spin up a database at any version. 在我的工作场所中,我们为应用程序的每个后续版本生成并存储一个创建脚本,以便我们可以随时启动任何版本的数据库。 This would be my suggested method of creating new databases (code to make the database on the server and then running the create script). 这是我建议的创建新数据库的方法(在服务器上创建数据库并运行创建脚本的代码)。 This code would run wherever you register new clients (perhaps delaying their registration notification until their database is ready?). 此代码将在您注册新客户端的任何地方运行(可能将其注册通知延迟到其数据库就绪之前?)。

I think that running 5000 databases out of one SQL Server instance is an unreasonable large number, and you should consider how to split up your databases onto separate servers and how to manage pointing to the correct host. 我认为在一个SQL Server实例中运行5000个数据库是不合理的,因此您应该考虑如何将数据库拆分到单独的服务器上以及如何管理指向正确主机的指向。

Given an connection string to an existing database on SQL Server, and the proper initialization setting ( CreateIfNotExist ), your DbContext will work its magic and create the database structure for you. 给定一个连接字符串到SQL Server上的现有数据库,以及适当的初始化设置( CreateIfNotExist ),你DbContext将工作它的魔力,并为您创建的数据库结构。 This of course will leave you lacking any non-table elements in your database (stored procedures, functions), so you would have to manage adding those after the fact (unless that is a feature of EF I don't know about). 当然,这将使您在数据库中缺少任何非表元素(存储过程,函数),因此您必须设法在事后添加这些元素(除非这是我不知道的EF的功能)。

You could also re-work your database structure so that all clients exist in one database with tables having a ClientID field. 您还可以重新设计数据库结构,以使所有客户端都存在于一个数据库中,其中的表具有一个ClientID字段。 Since I don't know the details of your application it would be hard to say if that is a good solution or not. 由于我不知道您的应用程序的详细信息,因此很难说这是否是一个好的解决方案。

Regarding EF and connection strings: 关于EF和连接字符串:

To specify a connection string in Entity Framework you have TWO options. 要在Entity Framework中指定连接字符串,您有两个选项。

Option 1. Pass the actual connection string (as a string) into the constructor of your DbContext. 选项1.将实际的连接字符串(作为字符串)传递到DbContext的构造函数中。 You can also do method two this way, but instead of passing in the actual connection string you pass in the NAME of the connection string in your app.config (or web.config). 您也可以使用这种方法进行第二种方法,但是您无需传入实际的连接字符串,而是在app.config(或web.config)中传入连接字符串的名称。

public class YourContext
{
public YourContext(string connectionString) : base(connectionString) { }
}

Option 2: Specify the connection string in the app.config of the application which is consuming your DbContext and pass in the NAME of the connection string in the app.config. 选项2:在消耗您的DbContext的应用程序的app.config中指定连接字符串,并在app.config中传入连接字符串的名称。 This is my preferred method, and I usually hard-code the name into the DbContext class. 这是我的首选方法,通常将名称硬编码到DbContext类中。

public class YourContext
{
  public YourContext() : base("YourContext") {}
}

in your App.Config 在您的App.Config中

<connectionStrings>
<add name="YourContext" connectionString="Data Source=YourDataSource(SQLServerInstance);Initial Catalog=YourDatabaseName;User Id=selfexplanatory;Password=selfexplanatory" providerName="System.Data.SqlClient" />
<add name="GenericYourContext" connectionString="Data Source=YourDataSource(SQLServerInstance);Initial Catalog={0};User Id=selfexplanatory;Password=selfexplanatory" providerName="System.Data.SqlClient" />
</connectionStrings>

The "Big" SQL Server, can be controled by connection Strings without any problems. 可以通过连接字符串控制“大” SQL Server,而不会出现任何问题。 Your DBContext just has to pass the name of the connectionString ( "name=yourconnectionStringNameHere" ) as parameter to it's superclass (in Constructor): 您的DBContext只需要将connectionString的名称( "name=yourconnectionStringNameHere" )作为参数传递给它的超类(在Constructor中):

class MyDBContext : DBContext {
    public MyDBContext(String customerConnectionString) : base("name="+customerConnectionString) {}
}

This will lead to a Database per customer which can be autocreated. 这将导致可以自动创建每个客户的数据库。 If you are using code-first (which i think, because you speak of autogenerating database) you (both of you ;)) should also take a look into EF-Migrations, which make change scripts on version updates and handle them in SQL for you. 如果您使用的是代码优先(我认为,因为您提到的是自动生成数据库),那么(你们俩;))还应该研究一下EF-Migrations,它们可以在版本更新中生成更改脚本,并在SQL中使用它们来处理您。

Hope this helps 希望这可以帮助

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

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