【问题标题】:Connection DB with entityFramework n-tier application与 entityFramework n 层应用程序的连接 DB
【发布时间】:2014-12-08 04:45:42
【问题描述】:

我正在构建一个餐厅预订系统,使用 asp.net mvc 4 entityframework、unity(用于 Ioc)和工作单元、存储库模式。所以我在 vs 2013 上为解决方案制作了 4 个项目:

  • RestaurentSyeteme(Web 项目 ASP.NET MVC 4)
  • RestaurentSystemeBusiness : 我把业务逻辑放在这一层
  • RestaurentSystemeDAL:数据访问、实体框架、存储库和上下文
  • RestaurentSystemeDomain:我的实体

这是我的 dbContext 的代码:

public class RestaurentDbSystem : UnitOfWork
{

    public RestaurentDbSystem(string nameOrConnStr)
        : base(nameOrConnStr)
    {

    }
}

这里是 UnitOfWork:

 public class UnitOfWork : DbContext, IUnitOfWork
  {
        public UnitOfWork(string nameOrConnString)
               : base(nameOrConnString)
        {

        }
  }

这是配置我的容器(Unity)的代码:

   private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        container.RegisterType<IUnitOfWork, RestaurentDbSystem>(new PerHttpRequestLifetime(),
            new InjectionConstructor("RestaurentDbSystem"));

        // e.g. container.RegisterType<ITestService, TestService>();            
        container.RegisterType<IClientService, ClientService>();
        container.RegisterType<IClientRepository, ClientRepository>();
        container.RegisterType<IRepository<User>, Repository<User>>();
        container.RegisterType<IRepository<UserType>, Repository<UserType>>();
        return container;
    }

这里是我在 web.config 中配置连接字符串的内容

<add name="RestaurentDbSystem" 
     connectionString="Data Source=LMA000049\mssqlserver;
     Initial Catalog=RestaurentSystemDb;Integrated Security=true"
     providerName="System.Data.SqlClient" />  

我创建了名为 ='RestaurentSystemDb' 的数据库。

问题是当执行应用程序时我有这个异常:

EntityFramework.dll 中出现“System.Data.ProviderIncompatibleException”类型的异常,但未在用户代码中处理

【问题讨论】:

  • 您的异常文本丢失。
  • 这个异常应该告诉你很多。没有这些细节就无法分析错误。

标签: entity-framework n-tier-architecture


【解决方案1】:

RestaurentDbSystempublic class RestaurentDbSystem : UnitOfWork

RestaurentSystemDb in Initial Catalog=RestaurentSystemDb

我认为这两个必须匹配。

通常,如果它们不相同,Code-First 会尝试在 SQL Express 上创建数据库。

【讨论】:

  • 我更改了数据库的名称,所以初始目录是:RestaurentDbSystem,但我仍然有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
相关资源
最近更新 更多