使用Entity Framework在运行时打开一个SQL CE文件4 [英] Opening an SQL CE file at runtime with Entity Framework 4

查看:139
本文介绍了使用Entity Framework在运行时打开一个SQL CE文件4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始使用Entity Framework 4,我创建了一个演示应用程序作为一个学习练习。该应用程序是一个简单的文档生成器,它使用SQL CE存储。每个文档项目都有自己的SQL CE数据文件,用户打开这些文件之一来处理项目。

I am getting started with Entity Framework 4, and I an creating a demo app as a learning exercise. The app is a simple documentation builder, and it uses a SQL CE store. Each documentation project has its own SQL CE data file, and the user opens one of these files to work on a project.

EDM非常简单。文件项目包括一个主题列表,每个主题都有标题,描述和零个或多个注释。所以,我的实体是主题,其中包含标题和文本属性以及具有标题和文本属性的注释。有一个一对多的关联从主题到注释。

The EDM is very simple. A documentation project is comprised of a list of subjects, each of which has a title, a description, and zero or more notes. So, my entities are Subject, which contains Title and Text properties, and Note, which has Title and Text properties. There is a one-to-many association from Subject to Note.

我试图找出如何打开一个SQL CE数据文件。数据文件必须与EF4创建数据库向导创建的SQL CE数据库的模式相匹配,我将在应用程序的其他位置实现一个新的文件用例来实现该要求。现在,我只想在应用程序中打开一个现有的数据文件。

I am trying to figure out how to open an SQL CE data file. A data file must match the schema of the SQL CE database created by EF4's Create Database Wizard, and I will implement a New File use case elsewhere in the app to implement that requirement. Right now, I am just trying to get an existing data file open in the app.

我已经转载了我现有的打开文件代码。我已将其设置为名为文件服务的静态服务类。代码还没有工作,但是足够显示我正在努力做什么。我试图保持ObjectContext为实体对象更新打开,在文件关闭时进行处理。

I have reproduced my existing 'Open File' code below. I have set it up as a static service class called File Services. The code isn't working quite yet, but there is enough to show what I am trying to do. I am trying to hold the ObjectContext open for entity object updates, disposing it when the file is closed.

所以,这是我的问题:我在正确的轨道上吗?我需要改变什么才能使这个代码与EF4一起工作?有没有正确的做法的例子?

So, here is my question: Am I on the right track? What do I need to change to make this code work with EF4? Is there an example of how to do this properly?

感谢您的帮助。

我现有的代码:

public static class FileServices
{
    #region Private Fields

    // Member variables
    private static EntityConnection m_EntityConnection;
    private static ObjectContext m_ObjectContext;

    #endregion

    #region Service Methods

    /// <summary>
    /// Opens an SQL CE database file.
    /// </summary>
    /// <param name="filePath">The path to the SQL CE file to open.</param>
    /// <param name="viewModel">The main window view model.</param>
    public static void OpenSqlCeFile(string filePath, MainWindowViewModel viewModel)
    {  
        // Configure an SQL CE connection string
        var sqlCeConnectionString = string.Format("Data Source={0}", filePath);

        // Configure an EDM connection string
        var builder = new EntityConnectionStringBuilder();
        builder.Metadata = "res://*/EF4Model.csdl|res://*/EF4Model.ssdl|res://*/EF4Model.msl";
        builder.Provider = "System.Data.SqlServerCe";
        builder.ProviderConnectionString = sqlCeConnectionString;
        var entityConnectionString = builder.ToString();

        // Connect to the model
        m_EntityConnection = new EntityConnection(entityConnectionString);
        m_EntityConnection.Open();

        // Create an object context
        m_ObjectContext = new Model1Container();

        // Get all Subject data
        IQueryable<Subject> subjects = from s in Subjects orderby s.Title select s;

        // Set view model data property
        viewModel.Subjects = new ObservableCollection<Subject>(subjects);
    }

    /// <summary>
    /// Closes an SQL CE database file.
    /// </summary>
    public static void CloseSqlCeFile()
    {
        m_EntityConnection.Close();
        m_ObjectContext.Dispose();
    }

    #endregion
}


推荐答案

这是答案。我简化了我的代码,并运行在更简单的EDM模型,迪斯尼角色。模型有两个实体, Character Child ,其中 Character Child 。孩子是角色的孩子 - 很简单的东西。我将演示文稿作为控制台应用程序来保持尽可能简单。

Here is the answer. I simplified my code and ran it on simpler EDM model, Disney Characters. Model has two entities, Character and Child, with a 1:* association between Character and Child. Children are character's kids--pretty simple stuff. I wrote the demo as a console app to keep it as simple as possible.

Program.cs 中的完整代码如下:

class Program
{
    static void Main(string[] args)
    {
        /* See http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/8a89a728-6c8d-4734-98cb-11b196ba11fd */

        // Configure an SQL CE connection string 
        var filePath = @"D:\Users\dcveeneman\Documents\Visual Studio 2010\Demos\SqlCeEf4Demo\SqlCeEf4Demo\DisneyChars.sdf";
        var sqlCeConnectionString = string.Format("Data Source={0}", filePath);

        // Create an EDM connection
        var builder = new EntityConnectionStringBuilder();
        builder.Metadata = "res://*/DisneyChars.csdl|res://*/DisneyChars.ssdl|res://*/DisneyChars.msl";
        builder.Provider = "System.Data.SqlServerCe.3.5";
        builder.ProviderConnectionString = sqlCeConnectionString;
        var edmConnectionString = builder.ToString();
        var edmConnection = new EntityConnection(edmConnectionString);

        // Build and query an ObjectContext
        using (var context = new DisneyCharsContainer(edmConnection))
        {
            var chars = context.Characters;
            foreach(var character in chars)
            {
                Console.WriteLine("Character name: {0}", character.Name);
                foreach(var child in character.Children)
                {
                    Console.WriteLine("Child name: {0}", child.Name);
                }
            }
            Console.ReadLine();
        }
    }
}

代码是我用来编写代码的论坛线程。

Link at the top of the code is to a forum thread that I used to write the code.

以下是演练:首先,创建一个数据库连接。由于我使用SQL CE,我没有连接字符串构建器 - 连接字符串只是一个路径,所以我不需要一个。然后我使用一个 EntityConnectionStringBuilder 构建一个实体连接字符串,然后我用它来构建一个 EntityConnection 。最后,我将连接传递给我的 ObjectContext 的构造函数。然后我可以使用 ObjectContext 来查询EDM。

Here is the walkthrough: First, create a database connection. Since I am using SQL CE, I don't have a connection string builder--the connection string is simply a path, so I don't need one. Then I use an EntityConnectionStringBuilder to build an entity connection string, and then I use that to build an EntityConnection. Finally, I pass the connection to the constructor for my ObjectContext. I can then use the ObjectContext to query the EDM.

这篇关于使用Entity Framework在运行时打开一个SQL CE文件4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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