如何创建实体连接

发布于 2024-11-09 10:40:20 字数 738 浏览 0 评论 0原文

我有一个包含多个项目的解决方案。

其中一个项目是我的 DataAccess 项目,定义了 2 个 SQL CE 数据库,一个用于本地用户,一个用于公司数据,两者都具有相似的表结构。为每个数据库定义了公共、静态、只读字符串,并且可以建立连接。

另一个项目是我的 WPF 项目,它将(最终)显示我的数据。为了在此处显示数据,我尝试创建一个实体上下文对象,但似乎没有任何效果。

是什么阻止我访问其他项目中的数据? (目前,错误表明“数据源”未定义)

如何修复?

AbcEntities abcContext;

public MainWindow() {
  InitializeComponent();
}

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) {
    MessageBox.Show(err.Message);
  }
}

I have a Solution with multiple projects.

One project is my DataAccess project with 2 SQL CE databases defined, one for local users and one for company data, both with similar table structures. Public, static, readonly strings are defined for each database, and connections can be made.

Another project is my WPF project that will (eventually) display my data. To display data here, I have tried creating an Entity Context object, but nothing seems to work.

What is preventing me from accessing my data in the other project? (Currently, error states 'data source' is not defined)

How do I fix it?

AbcEntities abcContext;

public MainWindow() {
  InitializeComponent();
}

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) {
    MessageBox.Show(err.Message);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2024-11-16 10:40:20

对于其他人,我找到了如何做到这一点。

在我的静态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();
      con.Open();
      Console.WriteLine("{0} Entity String created.", dbFileName);
      con.Close();
      return con.ConnectionString;
    } catch (Exception err) {
      Console.WriteLine(err);
    }
  }
  return null;
}

请注意,如果出现任何错误,则会返回 NULL 字符串。

如果有人想使用它,他们应该在异常处放置一个断点,抛出它,或者以某种方式处理它。 Console.WriteLine() 只是为了让我调试这个。

我为我的应用程序创建了实体连接字符串,如下所示:

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

希望其他人能从中受益。

For others, I found out how to do this.

In my static DataClass, I created the following routine to generate the Entity Connection String for me:

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();
      con.Open();
      Console.WriteLine("{0} Entity String created.", dbFileName);
      con.Close();
      return con.ConnectionString;
    } catch (Exception err) {
      Console.WriteLine(err);
    }
  }
  return null;
}

Notice that if there is any error, a NULL String is returned.

If anyone wants to use this, they should either place a breakpoint at the Exception, throw it, or handle it in some way. The Console.WriteLine() was just for me to debug through this.

I created Entity Connection Strings for my applications as follows:

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

Hope others get some mileage out of this.

醉酒的小男人 2024-11-16 10:40:20

有关如何创建 EntityConnection 的说明(实际上更多的是示例)可以在 MSDN 上找到:

http://msdn.microsoft.com/en-us/library/bb738533.aspx

看起来与您所拥有的非常相似。

Explanation (more of an example, really) of how to create an EntityConnection can be found on MSDN here:

http://msdn.microsoft.com/en-us/library/bb738533.aspx

Looks pretty similar to what you have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文