简体   繁体   English

实体框架和SQL Server 2008 Express

[英]Entity Framework & SQL Server 2008 Express

I'm totally new to Entity Framework and have done some reading, and as a test I have put together a very brief test framework with just two small Entities. 我是Entity Framework的新手,并且已经读了一些书,作为测试,我仅用两个小实体组成了一个非常简短的测试框架。 I then right clicked and selected "Generate Database from Model" which takes me to the SQL Connections page. 然后,我右键单击并选择“从模型生成数据库”,该页面将我带到“ SQL连接”页面。 However, none of the previous connections I have used appear in the drop down list, and when I select Create New Connection I only have the options to use 'SQL Server Compact 3.5' or 'SQL Server Database File'. 但是,我以前使用的任何连接都不会出现在下拉列表中,并且当我选择“创建新连接”时,我只有使用“ SQL Server Compact 3.5”或“ SQL Server数据库文件”的选项。

I have come across this before with SQL Express and the work around is to create my own Connection String to access the required Database. 我之前使用SQL Express遇到过这种情况,解决方法是创建自己的连接字符串以访问所需的数据库。 However, with me using Entity Framework to create the database, it is impossible to write an appropraite connetion string. 但是,使用我使用Entity Framework创建数据库时,不可能编写适当的连接字符串。

I therefore seem to be in a Catch 22 situation. 因此,我似乎处于Catch 22的情况。

  1. I cannot write a connection string until the database is created. 在创建数据库之前,我无法编写连接字符串。
  2. I cannot create the database from EF without accessing SQL Server (via a connection string). 如果不访问SQL Server(通过连接字符串),就无法从EF创建数据库。

Anyone come across this or can point out what I'm doing wrong. 任何人都会遇到这个问题,或者可以指出我做错了什么。 Like I said I'm totally new to EF so I apologise if this is a very basic question. 就像我说的那样,我对EF完全陌生,因此,如果这是一个非常基本的问题,我深表歉意。

Unless you want to use a database file in a user instance, you need to either use SQL Management Studio Express, or use the SQLCMD command line tool to create the database. 除非要在用户实例中使用数据库文件,否则需要使用SQL Management Studio Express或使用SQLCMD命令行工具来创建数据库。 I would recommend SQL Management Studio Express as it is easy to learn in my opinion. 我建议使用SQL Management Studio Express,因为我认为它很容易学习。

The Entity Framework tools are intended to be used to create the database schema , not the database itself. 实体框架工具旨在用于创建数据库架构 ,而不是数据库本身。 You still need to define the file groups, security information, and other basics of creating and configuring a blank database. 您仍然需要定义文件组,安全性信息以及创建和配置空白数据库的其他基础知识。

See this Q/A for appropriate links: 有关适当的链接,请参见此问题解答:

How to create DB in SQL Express using SQL commands? 如何使用SQL命令在SQL Express中创建数据库?

Thanks @psuedocoder, your assistance helped me resolve the problem. 感谢@psuedocoder,您的帮助帮助我解决了问题。 The answer in the end was simple, but not intuitive to me, hence my difficulty. 最后的答案很简单,但对我来说却不直观,因此很困难。 I thought some elaboration on the answer might help others who are equally new to Entity Framework as me. 我认为对答案的详细阐述可能会对像我一样对Entity Framework同样陌生的其他人有所帮助。

From Visual Studio 从Visual Studio

  1. Create your EF model in Visual Studio. 在Visual Studio中创建您的EF模型。
  2. Right click the model canvas and select 'Generate Database from Model'. 右键单击模型画布,然后选择“从模型生成数据库”。
  3. You are then asked to select the database connection. 然后要求您选择数据库连接。 Select 'New Connection'. 选择“新连接”。
  4. In the 'Add Connection' dialog box, rather than 'Browse' for an existing database, just simply type the name of your new database in the Database text box and Click Connect. 在“添加连接”对话框中,而不是在现有数据库的“浏览”中,只需在“数据库”文本框中键入新数据库的名称,然后单击“连接”。
  5. When you try to connect you will get a warning saying the database does not exist, but you will be asked if you want VS to 'Create It'. 当您尝试连接时,您会收到一条警告,指出该数据库不存在,但系统会询问您是否要VS“创建它”。 Select Yes. 选择是。

As @psuedocoder states this does not actually create the database in SQL Server, but it does create an object in your VS soluton which contains the TSQL script required in order to create your database. 正如@psuedocoder指出的那样,这实际上并没有在SQL Server中创建数据库,但确实在VS解决方案中创建了一个对象,该对象包含创建数据库所需的TSQL脚本。

From SSMS 从SSMS

  1. Go to SQL Server Management Studio and create a blank database of the same name used in step 4 above. 转到SQL Server Management Studio并创建一个空白数据库,其名称与上述步骤4中使用的名称相同。
  2. In SSMS select open file and navigate to the windows folder containing your VS solution files. 在SSMS中,选择打开文件,然后导航到包含VS解决方案文件的Windows文件夹。
  3. Open the TSQL script file. 打开TSQL脚本文件。 This will have a DB Script icon and have an .edmx extension. 这将具有一个DB脚本图标,并具有一个.edmx扩展名。
  4. Click Execute, and you will have your new database created from your EF model. 单击执行,您将根据EF模型创建新数据库。

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

相关问题 实体框架4:SQL Server 2008的性能不佳 - Entity Framework 4 : Bad performance with SQL Server 2008 实体框架和SQL Server Express瓶颈 - Entity Framework and SQL Server Express bottlenecks NET框架导致SQL Server Express 2008安装失败 - SQL Server Express 2008 Installation failure due to NET framework 使用Sync Framework 2.1将SQL Server 2008和SQL Server 2008 Express与WCF同步 - Using Sync Framework 2.1 to synchronise SQL Server 2008 and SQL Server 2008 Express with WCF 实体框架4和SQL Server 2008多个可能的外键 - Entity Framework 4 and SQL Server 2008 Multiple Possible Foreign Keys 实体框架与sqlce和sql server / sql express数据库一起使用的模型 - Entity Framework use model with sqlce and sql server/ sql express databases C#,实体框架和删除SQL Server 2008中的子寄存器 - C#, Entity Framework and delete child registers in SQL Server 2008 使用SQL Server Express的实体框架会缓慢添加记录 - entity-framework with SQL Server Express slow to add records 无法将实体框架连接到本地SQL Server Express - Can't connect Entity Framework to local SQL Server Express 连接字符串C#实体框架SQL Server Express - Connection string C# Entity Framework SQL Server Express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM