What is an EntityConnection and how do I make one?

c# entity-framework wpf


I have a solution that includes many projects.

My DataAccess project is one project that has two SQL CE databases defined—one for local users and one for enterprise data—both of which have comparable table configurations. Each database has specified public, static, read-only strings that may be used to establish connections.

My WPF project, which will ultimately show my data, is another project. I've tried creating an Entity Context object to show data here, but nothing appears to work.

Why can't I access my data in the other project, you ask? (At the time of writing, an error says that the term "data source" is not defined.)

How can I correct it?

AbcEntities abcContext;

public MainWindow() {

private void Window_Loaded(object sender, RoutedEventArgs e) {
  try {
    EntityConnection ec = new EntityConnection(DataClass.AbcConnectionString);
    abcContext= new AbcEntities(ec);
    listbox1.ItemsSource = from c in abcContext.Customers select c;
    abcCustomersBox.DisplayMemberPath = "Name";
  } catch (Exception err) {
5/23/2011 8:48:20 PM

Accepted Answer

I learned how to do this for others.

I developed the following procedure to produce the String for Entity Connection for me from my static DataClass:

static string BuildEntityConnString(string dbFileName, string resourceData, string password) {
  string resAll = @"res://*/";
  string dataSource = @"Data Source=|DataDirectory|\" + dbFileName;
  EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
  entityBuilder.Metadata = string.Format("{0}{1}.csdl|{0}{1}.ssdl|{0}{1}.msl", resAll, resourceData);
  entityBuilder.Provider = "System.Data.SqlServerCe.3.5";
  if (String.IsNullOrEmpty(password)) {
    entityBuilder.ProviderConnectionString = dataSource;
  } else {
    entityBuilder.ProviderConnectionString = dataSource + ";Password=" + password;
  using (EntityConnection con = new EntityConnection()) {
    try {
      con.ConnectionString = entityBuilder.ToString();
      Console.WriteLine("{0} Entity String created.", dbFileName);
      return con.ConnectionString;
    } catch (Exception err) {
  return null;

Keep in mind that a NULL String is returned in the event of any errors.

Anyone wishing to use this should either set a breakpoint at the Exception, throw it, or deal with it in some manner. TheConsole.WriteLine() was intended just for me to troubleshoot this.

For my applications, I designed Connection Strings for Entities as follows:

public static readonly string EntityConnString =
  BuildEntityConnString("sqlCeDb.sdf", "myModel", "abc123~Funky");

I hope this is useful to other people.

5/25/2011 2:41:20 PM

Popular Answer

On MSDN, you can get an explanation (more of an example, actually) on how to establish an EntityConnection here:


Similar to what you have in appearance.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow