简体   繁体   English

如何从SQL Server中填充一组实体框架代码优先POCOS(一次关闭)?

[英]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.

相关问题 如何告诉Entity Framework首先在代码中使用SQL Server Compact? - How do I tell Entity Framework to use SQL Server Compact in code first? 外部SQL服务器上的实体框架代码优先 - Entity Framework Code First on external SQL server 如何使用实体框架生成与SQL Server兼容的数据库(代码优先) - How to generate a SQL Server compatible database with Entity Framework (code first) 如何使用代码优先实体框架设置默认日期 - How do I set a default date using code first entity framework 首先从SQL移到Entity Framework代码 - Moving from SQL to Entity Framework Code First 实体框架代码优先:如何转换从数据库返回的一组实体? - Entity Framework Code First: How can I cast a set of entities returned from the database? 如何在Entity Framework Code First中加密数据? - How do I encrypt data in Entity Framework Code First? 如何在Entity Framework中设置SQL Server 2008的登录信息? - How do I set the login info for SQL Server 2008 in Entity Framework? 我是否需要SQL Server db_ddladmin权限才能实现实体框架代码优先,这是一个安全问题吗? - Do I need SQL Server db_ddladmin rights for a Entity Framework Code-First Approach and is it a security issue? 如何从Code First Entity Framework序列化枚举作为DataContract的一部分 - How do I serialize an enum as part of a DataContract from Code First Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM