>我正在尝试在 C# Interactive 中运行一个使用实体框架从本地数据库返回一些数据的方法。但它返回一个错误,指出可以在应用程序配置文件中找到名为"交互式控制台DBEntities"的连接字符串。 我首先使用数据库。 我使用选项"初始化与项目交互"从 C# 交互式开始。
以下是详细信息...
交互式控制台中的命令
#r "C:UsersPathInteractiveConsolepackagesEntityFramework.5.0.0libnet45EntityFramework.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.ComponentModel.DataAnnotations.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Core.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Data.Entity.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Runtime.Serialization.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Security.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Xml.Linq.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Data.DataSetExtensions.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1Microsoft.CSharp.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Data.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Net.Http.dll"
#r "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.6.1System.Xml.dll"
#r "InteractiveConsole.exe"
using InteractiveConsole;
using InteractiveConsole.Model;
using InteractiveConsole.DAL;
var context = new InteractiveConsoleDBEntities();
context.Employees.ToList();
然后我得到错误
No connection string named 'InteractiveConsoleDBEntities' could be found in the application config file.
+ System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
+ System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
+ System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(System.Type)
+ InternalSet<TEntity>.Initialize()
+ InternalSet<TEntity>.Include(string)
+ DbQuery<TResult>.Include(string)
+ System.Data.Entity.DbExtensions.Include<T>(IQueryable<T>, string)
+ System.Data.Entity.DbExtensions.Include<T, TProperty>(IQueryable<T>, Expression<Func<T, TProperty>>)
+ InteractiveConsole.DAL.EmployeeDAL.GetEmployeeList()
应用程序配置文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="InteractiveConsoleDBEntities" connectionString="metadata=res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)MSSQLLocalDB;attachdbfilename=|DataDirectory|DBInteractiveConsoleDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
数据库上下文
namespace InteractiveConsole.Model
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class InteractiveConsoleDBEntities : DbContext
{
public InteractiveConsoleDBEntities()
: base("name=InteractiveConsoleDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Employee> Employees { get; set; }
public DbSet<Person> People { get; set; }
}
}
带有方法的类
using System.Data.Entity;
namespace InteractiveConsole.DAL
{
public class EmployeeDAL
{
public static List<Employee> GetEmployeeList()
{
using (var context = new InteractiveConsoleDBEntities())
{
return context.Employees.Include(x => x.Person).ToList();
}
}
}
}
即时窗口中的相同项目工作正常
InteractiveConsole.DAL.EmployeeDAL.GetEmployeeList()
Count = 2
[0]: {System.Data.Entity.DynamicProxies.Employee_0D99EB301BB74EDFF2203163D6E8A936C70F24995F1639BF58D81DCCA671DEC0}
[1]: {System.Data.Entity.DynamicProxies.Employee_0D99EB301BB74EDFF2203163D6E8A936C70F24995F1639BF58D81DCCA671DEC0}
希望有人知道我做错了什么,可以帮助我。
多谢
在我终于让它工作之前,我为此挣扎了一段时间。
为采用连接字符串参数的构造函数创建一个新的分部类(例如名为 YourEntities.cs),其中包含新的重载(不要修改现有类,因为每当重新建模数据库时,它都会被覆盖):
using System.Data.Entity;
namespace YourNamespace.Models
{
public partial class YourEntities : DbContext
{
public YourEntities(string connectionString)
: base(connectionString)
{
}
}
}
然后,构建您的项目,右键单击它并单击"初始化与项目交互"。 打开 web.config/app.config 并将连接字符串复制到剪贴板。
在交互式窗口中,粘贴将 ConnStringHere 替换为连接字符串的内容,但不要按回车键:
var db = new YourNamespace.Models.YourEntities("ConnStringHere");
粘贴后,将连接字符串中的"
替换为"
,转到 C# 交互中的行尾,然后按 Enter 键。
然后,您应该能够在 C# 交互式窗口中使用 db,就像在应用中一样:
Print(db.Employees.Count());
我意识到这很旧,但我找到了一种方法,无需更改我的代码,也不创建任何代理或其他变通方法代码。我能够通过编辑交互式窗口本身的配置文件来完成这项工作。在这篇文章中查看我的答案:
项目在 C# 交互中找不到我的 EF 连接字符串
如果您的应用依赖于其他配置数据,则可能还必须添加它。对我来说,只需添加连接字符串就足够了。