[英]How do I populate a set of Entity Framework Code first POCOS from SQL server (once off)?
We have custom code that wipes and initializes our test database by creating a few objects and saving them to the DatabaseContext (we use Migrations for the schema, but not for the data). 我们有自定义代码,可通过创建一些对象并将其保存到DatabaseContext来擦除和初始化测试数据库(我们将迁移用于模式,但不用于数据)。 However, our tester has created a lot more data, and doesn't want to lose her test data when we do a clean deploy.
但是,我们的测试人员创建了更多的数据,并且不想在进行全新部署时丢失其测试数据。 I therefore have to figure out how to get the data from SQL Server into C# (once off), so it an be run with our data-initialization program.
因此,我必须弄清楚如何将数据从SQL Server导入C#(一次关闭),因此可以使用我们的数据初始化程序来运行它。
My initial approach was to manually copy-paste the data into C# object initializers, but this got old quickly, since there are 100+ rows to copy. 我最初的方法是手动将数据复制粘贴到C#对象初始化器中,但是很快就变旧了,因为要复制100多行。
My second approach was to create the objects using a SELECT statement in SQL Server, mixing the C# initializer syntax into the SQL SELECT statement, and then copying the C# strings generated by SQL Server back into Visual Studio. 我的第二种方法是使用SQL Server中的SELECT语句创建对象,将C#初始化程序语法混合到SQL SELECT语句中,然后将SQL Server生成的C#字符串复制回Visual Studio。
Although the second approach seems OK, I was wondering if there's an obvious solution (apart from simply running a SQL INSERT script every time) that I am missing. 尽管第二种方法似乎还可以,但是我想知道是否有一个我缺少的明显解决方案(除了每次仅运行SQL INSERT脚本之外)。
I ended up going with the approach mentioned in the question. 我最终选择了问题中提到的方法。 Generating C# object initialializers in SQL Select Statements.
在SQL Select语句中生成C#对象初始化程序。
A few things I picked up, mostly obvious: 我收到的几件事,大部分是显而易见的:
CAST AS VARCHAR for all numeric fields (although SQL Server will give error if you don't). 所有数字字段的CAST AS VARCHAR(尽管如果不这样做,SQL Server会给出错误)。
Dates: convert to VARCHAR and surround with DateTime.Parse() 日期:转换为VARCHAR并用DateTime.Parse()包围
Use a CASE statement to convert SQL bool value (1/0) to true/false as strings. 使用CASE语句将SQL bool值(1/0)转换为字符串的true / false。
Enums: generate a cast. 枚举:生成演员表。
If using autonumber ids, remember to prefix instance names with a letter(s). 如果使用自动编号ID,请记住在实例名称前加上字母。 Add the object to DataContext after initializing it.
初始化后,将对象添加到DataContext中。
Don't be afraid to reference your C# code in the generated code. 不要害怕在生成的代码中引用您的C#代码。 It isn't going to run in SQL Server in any case.
无论如何,它都不会在SQL Server中运行。
I would just create an SQL script that recreates the data in your DB. 我只是创建一个SQL脚本来重新创建数据库中的数据。
You can run this script through the commandline or through C#... 您可以通过命令行或C#运行此脚本。
You do have another option. 您还有其他选择。 Take a backup of the test database, load that backup into a new database on the same server as the target database, and when the migration is finished (including clearing the database) issue cross-database
INSERT...SELECT
statements. 备份测试数据库,将该备份加载到与目标数据库位于同一服务器上的新数据库中,并在完成迁移(包括清除数据库)后,发布跨数据库
INSERT...SELECT
语句。
The benefit of this approach is if the testing team gets another version they like, you can take a backup and overwrite the staging database with that allowing them to continually build more data. 这种方法的好处是,如果测试团队获得他们喜欢的另一个版本,则可以进行备份并覆盖登台数据库,从而使他们能够不断构建更多数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.