【问题标题】:Getting an unexpected error with c# lazy loading in N-Tier design getting在 N 层设计中使用 c# 延迟加载出现意外错误
【发布时间】:2015-12-07 01:51:04
【问题描述】:

我的web应用程序是根据Implementing a generic data access layer using Entity Framework设计的

我必须在我的 DAL 中使用以下代码才能加载我的通用存储库:

public virtual IList<T> GetAll(params Expression<Func<T, object>>[] navigationProperties)
{
    List<T> list;
    using (var context = new Corporate_WebEntities())
    {
        IQueryable<T> dbQuery = context.Set<T>();
        foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
            dbQuery = dbQuery.Include<T, object>(navigationProperty);
        list = dbQuery
            .AsNoTracking()
            .ToList<T>();
    }
    return list;
}

以及我的 app.config 文件中的以下内容:

...
<connectionStrings>
    <add name="Corporate_WebEntities"
        connectionString="
            metadata=
                res://*/Model.csdl|
                res://*/Model.ssdl|
                res://*/Model.msl;
            provider=System.Data.SqlClient;
            provider connection string=&quot;
                data source=(local);
                initial catalog=Corporate_Web;
                persist security info=True;
                user id=*****;
                password=*****;
                MultipleActiveResultSets=True;
                App=EntityFramework&quot;"
        providerName="System.Data.EntityClient" />
</connectionStrings>
...

我得到的错误是:

No connection string named 'Corporate_WebEntities' could be found in the application config file.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: No connection string named 'Corporate_WebEntities' could be found in the application config file.

Source Error: 
Line 34:                    dbQuery = dbQuery.Include<T, object>(navigationProperty);
Line 35: 
Line 36:                list = dbQuery
Line 37:                    .AsNoTracking()
Line 38:                    .ToList<T>();

Source File: Z:\_Profile Storage\Projects\CorporateWeb.API\CorporateWeb.API.DAL\GenericDataRepository.cs    Line: 36 

Stack Trace: 
[InvalidOperationException: No connection string named 'Corporate_WebEntities' could be found in the application config file.]
   System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel() +288
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +466
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +53
   System.Data.Entity.Internal.Linq.InternalSet`1.AsNoTracking() +18
   System.Data.Entity.Infrastructure.DbQuery`1.AsNoTracking() +46
   System.Data.Entity.QueryableExtensions.AsNoTracking(IQueryable`1 source) +130
   CorporateWeb.API.DAL.GenericDataRepository`1.GetAll(Expression`1[] navigationProperties) in Z:\_Profile Storage\Projects\CorporateWeb.API\CorporateWeb.API.DAL\GenericDataRepository.cs:36
   CorporateWeb.API.BLL.BusinessLogicLayer_Settings.GetCompanyContactInformation() in Z:\_Profile Storage\Projects\CorporateWeb.API\CorporateWeb.API.BLL\BusinessLogicLayer_Settings.cs:375
   CorporateWeb.API.WebApiApplication.Session_Start() in Z:\_Profile Storage\Projects\CorporateWeb.API\CorporateWeb.API\Global.asax.cs:32

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
   System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +192
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +136
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +21
   System.Web.Util.ArglessEventHandlerProxy.Callback(Object sender, EventArgs e) +56
   System.Web.SessionState.SessionStateModule.RaiseOnStart(EventArgs e) +9916528
   System.Web.SessionState.SessionStateModule.CompleteAcquireState() +160
   System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) +1095
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +254
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我不明白的是我定义了Corporate_WebEntities 并且应用程序编译了。

  1. 我的 app.config 中缺少什么?
  2. 我还缺少什么其他“东西”?

编辑一个 我的 Model.Context.cs(Corporate_WebEntities 所在的位置):

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CorporateWeb.API.DAL
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using CorporateWeb.API.Model;

    public partial class Corporate_WebEntities : DbContext
    {
        public Corporate_WebEntities()
            : base("name=Corporate_WebEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Accounts> Accounts { get; set; }
        ...
    }
}

【问题讨论】:

  • 你的 exe 的名称是什么?你的 .exe.config 的名称是什么?它们在同一个文件夹中吗?
  • Corporate_WebEntities 的构造函数是什么样的?
  • @BrendanGreen,这个文件是我在项目中添加 EDMX 文件时自动生成的。
  • @TMcKeown - BIN 文件夹中的所有 DLL 都没有可执行文件
  • Bin 文件夹???就像在 ASP.NET 中一样?

标签: c# entity-framework generics n-tier-architecture


【解决方案1】:

您正在运行一个 Web 应用程序,请将这些连接字符串放入您的 WEB.CONFIG

【讨论】:

  • 天哪,我不敢相信我忘了这样做。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 2019-12-12
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
相关资源
最近更新 更多