C#互动和实体框架 [英] C# Interactive and Entity Framework
问题描述
我试图在C#Interactive中运行一个方法,该方法使用Entity Framework从本地数据库返回一些数据。但是它返回一个错误,提示可以在应用程序配置文件中找到名为 InteractiveConsoleDBEntities的连接字符串。
我首先使用数据库。
我使用选项使用项目初始化Interactive从C#Interactive开始。
以下是详细信息...
交互式控制台中的命令
#r C:\Users\Path\InteractiveConsole\ \packages\EntityFramework.5.0.0\lib\net45\EntityFramework.dll
#r C:\Program Files(x86)\参考汇编AssMicrosoft\Framework\。 NETFramework\v4.6.1\System.dll
#r C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\System .ComponentModel.DataAnnotations.dll
#r C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\System.Core.dll
#r C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\System.Data.Entity.dll
# r C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\System.Runtime.Serialization.dll
#r C:\ \程序文件(x86)\参考组件blies\Microsoft\Framework\.NETFramework\v4.6.1\System.Security.dll
#r C:\Program Files(x86)\Reference Assemblys\Microsoft\Framework \.NETFramework\v4.6.1\System.Xml.Linq.dll
#r C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETFramework\ \v4.6.1\System.Data.DataSetExtensions.dll
#r C:\程序文件(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\ \Microsoft.CSharp.dll
#r C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\System.Data.dll
#r C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll
# r C:\程序文件(x86)\参考程序集\Microsoft\Framework\.NETFramework\v4.6.1\System.Xml.dll
#r InteractiveConsole.exe
使用InteractiveConsole;
使用InteractiveConsole.Model;
使用InteractiveConsole.DAL;
var context = new InteractiveConsoleDBEntities();
context.Employees.ToList();
然后我得到了错误
在应用程序配置文件中找不到名为 InteractiveConsoleDBEntities的连接字符串。
+ 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(IQueryable< T,字符串)
+ System.Data.Entity.DbExtensions.Include< T,TProperty>(IQueryable< T,Expression< Func< T,TProperty>)
+ InteractiveConsole.DAL.EmployeeDAL.GetEmployeeList()
App.config文件
<?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版本= 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连接字符串=&数据源=(LocalDB)\MSSQLLocalDB; attachdbfilename = | DataDirectory | \DB\InteractiveConsoleDB.mdf;集成安全性= True;连接超时= 30; MultipleActiveResultSets = True; App = EntityFramework& providerName = System.Data.EntityClient />
< / connectionStrings>
< / configuration>
DbContext
namespace InteractiveConsole.Model
{
使用系统;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
公共部分类InteractiveConsoleDBEntities:DbContext
{
public InteractiveConsoleDBEntities()
:base( name = InteractiveConsoleDBEntities)
{
}
受保护的覆盖无效OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet< Employee>员工{得到;组; }
public DbSet< Person>人们{组; }
}
}
带有方法的类
使用System.Data.Entity;
名称空间InteractiveConsole.DAL
{
public class EmployeeDAL
{
public static List< Employee> GetEmployeeList()
{
使用(var context = new InteractiveConsoleDBEntities())
{
返回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_0D99EB301BB74EDFF2203163D6E8A693 $ p>
希望有人知道我做错了什么,可以为我提供帮助。
非常感谢
解决方案我意识到这已经很老了,但是我找到了一种无需更改代码或创建任何代理或其他解决方法的代码即可完成此工作的方法。我可以通过编辑交互式窗口本身的配置文件来完成这项工作。看到我在这篇文章中的答案:
您可能有也可以添加其他配置数据(如果您的应用依赖)。对我来说,只需添加连接字符串就足够了。
I am trying to run a method in C# Interactive that return some data from local db using Entity Framework. But it return an error saying that the connection string named 'InteractiveConsoleDBEntities' could be found in the application config file.
I am using data base first.
I use the option "Initialize Interactive with project" to start with C# Interactive.
Here is the details...
Commands in Interactive Console
#r "C:\Users\Path\InteractiveConsole\packages\EntityFramework.5.0.0\lib\net45\EntityFramework.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.ComponentModel.DataAnnotations.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Core.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Data.Entity.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Runtime.Serialization.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Security.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Xml.Linq.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Data.DataSetExtensions.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Microsoft.CSharp.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Data.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Net.Http.dll"
#r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Xml.dll"
#r "InteractiveConsole.exe"
using InteractiveConsole;
using InteractiveConsole.Model;
using InteractiveConsole.DAL;
var context = new InteractiveConsoleDBEntities();
context.Employees.ToList();
Then I get the error
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()
The App.config file
<?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|\DB\InteractiveConsoleDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
The DbContext
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; }
}
}
The class with method
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();
}
}
}
}
The same project in Immediate Window works fine
InteractiveConsole.DAL.EmployeeDAL.GetEmployeeList()
Count = 2
[0]: {System.Data.Entity.DynamicProxies.Employee_0D99EB301BB74EDFF2203163D6E8A936C70F24995F1639BF58D81DCCA671DEC0}
[1]: {System.Data.Entity.DynamicProxies.Employee_0D99EB301BB74EDFF2203163D6E8A936C70F24995F1639BF58D81DCCA671DEC0}
Hope some one know what I doing wrong and can help me.
Thanks a lot
解决方案 I realize this is old, but I found a way to make this work without changing my code, or creating any proxies or other work-around code. I was able to make this work by editing the config file for the interactive window, itself. See my answer in this post:
Project can't find my EF connection string in C# Interactive
You may have to add other config data, as well, if your app relies on it. Just adding the connection string was enough, for me.
这篇关于C#互动和实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!