【问题标题】:Entity Framework can't find my connection string实体框架找不到我的连接字符串
【发布时间】:2016-12-20 14:58:50
【问题描述】:

只需调用我的 WCF 来填充我的 datagridView

private void button1_Click(object sender, EventArgs e)
{
    ServiceReferenceReservations.ReservationsServiceClient srr = 
               new ServiceReferenceReservations.ReservationsServiceClient();
                        gridData.DataSource = srr.getAllReservations();
}

这就是 mycf 所做的将业务层的返回类型转换为正确的返回类型

public List<clsReservation> getAllReservations()
{
    List<clsReservation> oDataList = new List<clsReservation>().ToList();

    List<Reservation> mesReservations = BusinessLayer.Reservations.LoadAllReservationsEF();

    foreach (var item in mesReservations)
    {
        clsReservation cls = new clsReservation()
                {
                    id = item.id,
                    lecteurID = item.lecteurID,
                    livreID=item.livreID
                };
        oDataList.Add(cls);
    }

    return oDataList;
}    

业务层将调用数据访问层并返回数据

return DataAccessLayer.Reservations.LoadAllReservationEF();

那我的数据访问层使用的是实体框架

public static List<Reservation> LoadAllReservationEF()
{
    List<Reservation> malisteReservation = new List<Reservation>();

    using (bibliothequeEntities dbcontext = new bibliothequeEntities())
    {
        List<Reservation_SelectAll_Result> maliste = dbcontext.Reservation_SelectAll().ToList();

        var x = from p in maliste
                select new Reservation
                        {
                            id = p.id,
                            lecteurID = p.lecteurID,
                            livreID = p.livreID,
                        };

        foreach (var item in x)
        {
            malisteReservation.Add(item);
        }
    }

    return malisteReservation;
}

我的数据访问层在 Model1.Context.cs 中抛出错误:

在 DAL 的应用程序配置文件中找不到名为“bibliothequeEntities”的连接字符串

<connectionStrings>
    <add name="bibliothequeEntities" 
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=arpa;initial catalog=bibliotheque;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

但我的 DAL 中以及调用 WCF 的启动项目中都有该连接字符串。我已经尝试评论方法“onModelCreating”以避免抛出错误但仍然找不到解决方案

我错过了什么?

【问题讨论】:

  • 请显示bibliothequeEntities 的构造函数(只是声明和任何base 调用)
  • public bibliothequeEntities() : base("name=bibliothequeEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // throw new UnintentionalCodeFirstException(); }
  • 您所指的 .config 文件肯定是 主应用程序的 配置文件,而不是 bibliothequeEntities 所在的程序集的配置文件,如果不同的话?跨度>
  • 我有 6 个项目
  • 我有 6 个项目 GUI、WCF 库、BAL、DAL 和模型在 DAL 但你可以看到 GUI 只能通过 wcf 访问其他库

标签: c# entity-framework wcf connection-string


【解决方案1】:

如果托管在 IIS 中,您需要将 app.config 文件的内容复制到您在 IIS 中托管它的虚拟目录的 web.config 文件中。我不相信这是自动为您完成的。

如果您使用其他东西来托管,您同样需要找到主机正在执行的文件夹,并将内容复制到 那个 app/web.config。

简而言之,您的连接字符串需要在 host 的 .config 中,而不是任何其他程序集。

更多详情请见here

【讨论】:

  • 它只是自己托管的,我没有配置 II 或其他任何东西,它只是一个以 winforms 作为客户端的简单 wcf
  • 公共虚拟 ObjectResult Reservation_SelectAll() { return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("Reservation_SelectAll"); }
  • 这是抛出由entitframework产生的错误的方法
  • 您需要了解更多关于如何托管 WCF 库的信息,然后才能确定将 .config 设置复制到何处。如果您只是从 Visual Studio 运行,它可能会托管在 WCF 服务主机中,但这只是一个 Visual Studio 帮助程序,不适合生产使用。有很多信息here,包括您选择每个选项的原因。
  • 我认为我真正的问题是与未安装在其他层上的实体的链接,它只是安装在 DAL 中
【解决方案2】:

连接字符串应该在wcf中,这就是全部,甚至不需要将它放在dataccesslayer中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多