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
private static EntityConnection m_EntityConnection;
private static ObjectContext m_ObjectContext;
#endregion
#region Service Methods
public static void OpenSqlCeFile(string filePath, MainWindowViewModel viewModel)
{
var sqlCeConnectionString = string.Format("Data Source={0}", filePath);
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();
m_EntityConnection = new EntityConnection(entityConnectionString);
m_EntityConnection.Open();
m_ObjectContext = new Model1Container();
IQueryable<Subject> subjects = from s in Subjects orderby s.Title select s;
viewModel.Subjects = new ObservableCollection<Subject>(subjects);
}
public static void CloseSqlCeFile()
{
m_EntityConnection.Close();
m_ObjectContext.Dispose();
}
#endregion
}