目前我的解决方案中有 2 个项目,一个 ASP.NET Core Web 应用程序和一个 .NET 4.6.1(非核心)类库。在我的类库中,我添加了 Entity Framework 6,并使用第一代数据库创建了我所有的数据库上下文类。在类库中,一切都按预期工作;但是,当我尝试从 .NET Core Web App 访问 ef 类时,它没有可供使用的连接字符串并且失败了。
现在,以旧的方式,我知道我可以将连接字符串设置从类库中的 app.config 复制到 Web 应用程序中的 web.config,但 .NET Core 不支持此操作。
我已经尝试使用概述的步骤 here ,但这仅适用于 Code-First 数据库生成(我没有这样做)。我也考虑过改用 EntityFrameworkCore,但我无法让 db-first generation 发生(使用这个 overview )。
有什么方法可以实现我想要做的事情吗?
最佳答案
下面是如何在代码库中为 EF 模型提供整个配置以及如何在运行时提供连接字符串。我最近也为此苦苦挣扎,因为这方面的文档有点难找。
假设您从数据库(DB-First)生成模型:MyEntities.edmx
您的项目中将有一个分部类,如下所示:
public partial class MyEntities : DbContext
{
public MyEntities ()
: base("name=MyEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<MyTable> MyTable { get; set; }
}
当您在 VS 中生成模型时,如果您选择将连接字符串存储在配置文件中,自动代码生成会将连接字符串配置名称放入构造函数中,如上所示:public MyEntities(): base("name=MyEntities")
在你的项目配置中,你会有这样的东西:
<configuration>
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://MyProject/MyEntities.csdl|res://MyProject/MyEntities.ssdl|res://MyProject/MyExchangeEntities.msl;provider=System.Data.SqlClient;provider connection string='data source=SERVERADDRESS;initial catalog=DATABASENAME;persist security info=True;user id=USERID;password=PASSWORD;MultipleActiveResultSets=True;App=EntityFramework'" />
</connectionStrings>
</configuration>
现在绕过这个,如果你看一下 DbContext 构造函数:public DbContext(string nameOrConnectionString);
它实际上说您可以将连接字符串直接传递给它。
因此,您需要做的就是扩展这个部分类并添加另一个采用连接字符串的构造函数。
public partial class MyEntities
{
/// <summary>
/// This constructor allows us to pass connection strings at runtime
/// </summary>
/// <param name="connectionString">Entity Framework connection string</param>
public MyEntities(string connectionString) : base(connectionString)
{
}
}
虽然这很明显,但我只想提一下,我们扩展了部分类而不是修改自动生成的部分类,这样它就不会在我们将来重新生成模型时被覆盖。
要使用您的连接字符串进行连接,您只需像这样构造您的对象:new MyEntities("metadata=res://MyProject/MyEntities.csdl|res://MyProject/MyEntities.ssdl|res://MyProject/MyExchangeEntities.msl;provider=System.Data.SqlClient;provider connection string='data source=SERVERADDRESS;initial catalog=DATABASENAME;persist security info=True;user id=USERID;password=PASSWORD;MultipleActiveResultSets=True;App=EntityFramework'");
最后,如果您还想在代码库中提供 EF 配置,可以这样做:
/// DbConfiguration belongs to System.Data
public class EntityFrameworkConfig : DbConfiguration
{
public EntityFrameworkConfig()
{
//We set configurations for entity framework here so that we don't have to add it in the app.config
this.SetDefaultConnectionFactory(new SqlConnectionFactory());
this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
}
}
/// <summary>
/// This class with the DbConfigurationType attribute allows EntityFramework to know
/// to get its config from the EntityFrameworkConfig class instead of the app.config
/// </summary>
[DbConfigurationType(typeof(EntityFrameworkConfig))]
public partial class MyEntities
{
/// <summary>
/// This constructor allows us to pass connection strings at runtime
/// </summary>
/// <param name="connectionString">Entity Framework connection string</param>
public MyEntities(string connectionString) : base(connectionString)
{
}
}
关于c# - 从 ASP.NET Core Web 应用访问类库中的 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37814615/