【问题标题】:Entity framework connection string reference from another project来自另一个项目的实体框架连接字符串引用
【发布时间】:2012-12-15 11:04:32
【问题描述】:

我有一个由 4 个项目组成的解决方案。 MVC、WCF、业务 LYR、DataAcess。我正在使用实体框架进行数据库事务。我的要求是我只想从 MVC webconfig 中获取实体连接字符串,而不在访问层的 APP.cofig 中引用。在这种情况下可能吗?

当我尝试以下代码时出现错误。

    this.ConnectionString="data source=cmh-sosql;initial catalog=Student;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";

            System.Data.SqlClient.SqlConnectionStringBuilder scsb = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);

EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder();
ecb.Metadata = "res://*/schoolModel.csdl|res://*/schoolModel.ssdl|res://*/schoolModel.msl";
ecb.Provider = "System.Data.SqlClient";
ecb.ProviderConnectionString = scsb.ConnectionString;

using (SchoolDB schoolDB = new SchoolDB(ecb.ConnectionString))

错误:实体类型 student 不是当前上下文模型的一部分。

【问题讨论】:

  • 你能解释一下为什么需要这个吗?即没有从 App.config 获取?
  • 我需要从第一个连接的数据库中获取第二个连接。那是我的问题...现在解决了...

标签: entity-framework c#-4.0 linq-to-sql entity linq-to-objects


【解决方案1】:

你完全正确。我得到了解决方案。无需在 webconfig 中保留任何字符串以引用实体模型。我们可以参考上面的代码。但改变的是配置上下文对象。

public SchoolDB(string  connectionString)
        : base(connectionString)
    {
    }
We need to change the constructor also by this format. 

感谢 Sampath

【讨论】:

  • 很高兴为您提供帮助。但请接受您的回答作为解决方案。然后其他人可以快速确定解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
相关资源
最近更新 更多