Opening a SQL CE file at run time with Entity Framework 4

I am starting with Entity Framework 4 and I am creating a demo application as a training exercise. The application is a simple documentation designer and uses SQL CE repository. Each documentation project has its own SQL CE data file, and the user opens one of these files to work on the project.

EDM is very simple. The draft documentation consists of a list of items, each of which has a name, description and zero or more notes. So, my objects are Subject, which contains the Title and Text and Note properties, which have the Title and Text properties. There is a one-to-many association from Topic to Note.

I am trying to figure out how to open a SQL CE data file. The data file must conform to the SQL CE database schema created by the EF4 Create Database Wizard, and I will implement the case of using a new file elsewhere in the application to implement this requirement. Right now, I'm just trying to open an existing data file in an application.

I reproduced the existing Open File code below. I installed it as a static service class called File Services. The code is not working yet, but enough to show what I'm trying to do. I am trying to open an ObjectContext to update object objects, saving it when the file is closed.

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

Thank you for your help.

My existing code is:

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
}
+3
2

. EDM, Disney Characters. , Character Child, 1: * Character Child. - - . , .

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();
        }
    }
}

, .

: . SQL CE, - - , . EntityConnectionStringBuilder , EntityConnection. , ObjectContext. ObjectContext EDM.

+6

/ SQL Server CE - . , , EF.

0

Source: https://habr.com/ru/post/1736685/


All Articles