【问题标题】:Entity Framework without App.config, Database first, Repository Pattern, N-tier Solution Architecture没有 App.config 的实体框架,数据库优先,存储库模式,N 层解决方案架构
【发布时间】:2019-01-11 00:59:37
【问题描述】:

好的,现在我放弃了。我的挑战是使用实体框架 6、存储库模式和 N 层解决方案架构构建通用 DAL。

简介: 使用这种方法https://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework,我创建了我的 DAL 和 BLL。 一切都好,单元测试顺利完成。不错。

挑战: 然后我创建了一个表示层,其中包括一个 Excel 插件。但是由于每个客户端使用的数据库可能不同,因此需要从客户端更改数据库 ConnectionString。 好的,将 app-config 添加到 Excel 模板项目中……不起作用。不管我做什么,我都没有从数据库中取回数据。

现在我需要删除对 app.config 的需求,而只需在初始化 BL 时从客户端提供 ConnectionString。

我尝试了Entity Framework - Database First without config的建议 和How can l use Entity Framework without App.config,但我似乎无法让它很好地适应我的挑战。我确实使用了其中提到的一些解决方案,但由于我的 DAL 使用的是存储库模式,因此我无法完全理解如何实施提到的解决方案......

我做了什么: 我在 DataAccessLayer 中添加了一个 DbConfiguration 类,它实现了多个构造函数。 (我无法弄清楚如何击中其他构造函数 - 只有参数构造函数,但这是另一个问题的问题)。 我的 ythis 类的构造函数如下所示:

private DefaultConfiguration()
{

    SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder()
    {
        DataSource = database,
        InitialCatalog = initialCatalog,
        UserID = userId,
        Password = password,
        IntegratedSecurity = useTrustedConnection
    };

    // Initialize the EntityConnectionStringBuilder.
    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder()
    {
        Provider = providerName,
        // Set the provider-specific connection string.
        ProviderConnectionString = sqlBuilder.ToString(),
        // Set the Metadata location.
        Metadata = @"res://*/CommonModel.csdl|res://*/CommonModel.ssdl|res://*/CommonModel.msl"
    };

    SetExecutionStrategy("System.Data.SqlClient", () => new DefaultExecutionStrategy(), database);      
}

EntityConnection entityCon = new EntityConnection(entityBuilder.ToString());
//SetDefaultConnectionFactory(new SqlConnectionFactory(entityCon.ConnectionString));
//SetDefaultConnectionFactory(new SqlConnectionFactory(sqlBuilder.ToString()));

我似乎无法弄清楚如何在我的 DbConfiguration 的初始化中使用我的 EntityConnection 对象。最后两行代码似乎都没有正常工作。

在我的 DbContext 中,我从

更改了构造函数
public CommonEntities()
    : base("name=CommonEntities")
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.ProxyCreationEnabled = false;
}

public CommonEntities(string ConnectionString)
    : base(ConnectionString)
{
    this.Configuration.LazyLoadingEnabled = false;
    this.Configuration.ProxyCreationEnabled = false;
}

不过,这只会引发可怕的 UnIntentionalCodeFirstException,我现在被卡住了。

问题:

如何从我的 PL 一直到 DAL 获取 ConnectionString?我会尝试创建一个非无参数存储库构造函数,它将接受一个 SQL 连接字符串作为参数,但是如何?

我知道这是一个很大的问题,但我已尝试在不引起 SystemOutOfMemoryException 的情况下提供尽可能多的信息,并尝试将我的问题归结为一个问题 - 稍后会出现更多问题,但现在这个应该够了……

希望你们中的一些神童能给我指点一二……

【问题讨论】:

  • 这不是一个答案,但是 EF 已经是一种存储库模式,并且 99.99% 的情况下,当我看到它们在使用中时,它们会产生更多的问题,然后才能解决
  • 您将拥有多少个不同的数据库?如果这个数字很小,我会为每个数据库设置一个 DAL 进程,并拥有自己的配置文件。
  • 我完全同意 - 感谢您的意见,如果涉及到这一点,我可能会直接忽略整个 repo 模式,但现在,我希望它能够工作 :)
  • @OlivierMATROT - 重点是消除对配置文件的需求。
  • 可能你不需要,这就是我问的原因。

标签: c# entity-framework-6 repository-pattern n-tier-architecture


【解决方案1】:

好的,我从来没有像我想要的那样工作,所以我将我的 DomainModel 更改为不使用 EDMX 文件 - 这是我得到的最终错误的全部原因......

基本上我已经将 EF 建模从 Database First 更改为 Code-First....

我还在 Excel-Addin 项目中添加了 EntityFramework 包。

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多