实体框架4和SQL Compact 4:如何生成数据库? [英] Entity Framework 4 and SQL Compact 4: How to generate database?

查看:119
本文介绍了实体框架4和SQL Compact 4:如何生成数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Model First方法开发具有Entity Framework 4和SQL Compact 4的应用程序。我创建了我的EDM,现在我想生成一个SQL Compact 4.0数据库,作为模型的数据存储。我打开生成数据库向导,然后单击新建连接按钮为生成的文件创建一个连接。出现选择数据源对话框,但SQL Compact 4.0未列在可用数据源列表中:





我正在运行VS 2010 SP1(beta)和I已经安装了用于SQL Compact 4.0的VS 2010 Tools。我可以从服务器资源管理器创建一个SQL Compact 4.0数据连接。只有在生成数据库向导中,4.0选项不会显示。 BTW,我的SQL Compact 4.0安装包括 System.Data.SqlServerCe.Entity.dll 。所以我应该有我需要的SQL Compact组件。



我做错了什么事情,还是这个bug?有没有人有修复或解决方法?谢谢你的帮助。

解决方案

作为解决方法,我使用生成数据库向导来生成一个SQL Compact 3.5脚本,我正在运行一个在VS 2010解决方案资源管理器中创建的一个空白的SQL Compact 4.0数据库。以下是完成该任务的步骤:



步骤1:运行生成数据库向导。它提示在其第一页上创建一个文件,因此创建一个SQL Compact 3.5文件。这个文件是一个虚拟的,所以你的名字不重要。



步骤2:完成向导创建DDL脚本这将配置新的数据库。



步骤3:删除步骤1中创建的虚拟文件。



步骤4:使用VS 2010的Server Explorer创建您将在项目中使用的实际SQL Compact 4.0文件。



步骤5:生成数据库向导在EDMX文件中放置了对SQL Compact3.5的两个引用;这些引用需要改为4.0。在VS 2010的XML编辑器中打开EDMX文件(右键单击并选择打开方式进入XML编辑器)。第7行应包含3.5引用。将这些更改为4.0并保存文件。



步骤6:在VS 2010中打开生成的EDMX.SQLCE脚本文件。VS将指示(通过工作窗格中的横幅横幅)脚本断开连接。右键单击横幅上方的空白区域,然后在出现的上下文菜单中选择连接>连接。这将打开一个连接对话框 - 使用它将脚本连接到您在步骤4中创建的SQL Compact 4.0数据库。



步骤7: strong>再次右键单击脚本空白处,并从上下文菜单中选择执行SQL 。脚本将执行,您将在脚本下方的窗格中获取通常的结果消息。假设成功执行,数据库将被配置为匹配实体数据模型。



步骤8:我们还需要确保App.config /web.config没有指向虚拟文件。如果将其更改为上述步骤4中创建的实际CE 4.0文件。否则当我们改变模型时,我们有麻烦的脚本的再生。在配置文件中,更改3.5到4.0。



在这一点上,您应该可以使用Entity Framework 4来处理数据库。


I am developing an app with Entity Framework 4 and SQL Compact 4, using a Model First approach. I have created my EDM, and now I want to generate a SQL Compact 4.0 database to act as a data store for the model. I bring up the Generate Database Wizard and click the New Connection button to create a connection for the generated file. The Choose Data Source dialog appears, but SQL Compact 4.0 is not listed in the list of available data sources:

I am running VS 2010 SP1 (beta) and I have installed the VS 2010 Tools for SQL Compact 4.0. I can create a SQL Compact 4.0 data connection from the Server Explorer. It is only in the Generate Database Wizard that the 4.0 option doesn't appear. BTW, my SQL Compact 4.0 installation does include System.Data.SqlServerCe.Entity.dll. So I should have the SQL Compact components I need.

Am I doing something incorrectly, or is this a bug? Does anyone have a fix or a workaround? Thanks for your help.

解决方案

As a workaround, I am using the Generate Database Wizard to generate a a SQL Compact 3.5 script, which I am running against a blank SQL Compact 4.0 database created in the VS 2010 Solution Explorer. Here are the steps to accomplish that task:

Step 1: Run the Generate Database Wizard. It prompts for the creation of a file on its first page, so have it create a SQL Compact 3.5 file. This file is a dummy, so it doesn't matter what you name it.

Step 2: Complete the wizard to create the DDL script that will configure the new database.

Step 3: Delete the dummy file created in Step 1.

Step 4: Use VS 2010's Server Explorer to create the actual SQL Compact 4.0 file that you will use in your project.

Step 5: The Generate Database Wizard places two references to SQL Compact "3.5" in the EDMX file; these references need to be changed to "4.0". Open the EDMX file in VS 2010's XML Editor (right-click and select 'Open With' to get to the XML Editor). Line 7 should contain the "3.5" references. Change these to "4.0" and save the file.

Step 6: Open the generated EDMX.SQLCE script file in VS 2010. VS will indicate (via a banner across the botton of the work pane) that the script is disconnected. Right-click on the white space above the banner and select Connection > Connect in the context menu that appears. That brings up a connection dialog--use it to connect the script to the SQL Compact 4.0 database that you created in Step 4.

Step 7: Right-click in the script white-space again and select Execute SQL from the context menu. The script will execute and you will get the usual results message in a pane below the script. Assuming successful execution, the database is then configured to match the Entity Data Model.

Step 8: We also need to ensure the App.config/web.config is not pointing to the dummy file. If it is change it to the actual CE 4.0 file created in Step 4 above. Else we have trouble with regeneration of the script when we make change to the model. In the config file change 3.5 to 4.0 as well.

At that point, you should be able to use Entity Framework 4 to work with your database.

这篇关于实体框架4和SQL Compact 4:如何生成数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆