C#Interactive和Entity Framework

时间:2018-02-24 14:11:50

标签: entity-framework c#-interactive

我正在尝试在C#Interactive中运行一个方法,该方法使用Entity Framework从本地数据库返回一些数据。但它返回一个错误,指出可以在应用程序配置文件中找到名为“InteractiveConsoleDBEntities”的连接字符串。 我首先使用数据库。 我使用“使用项目初始化交互”选项从C#Interactive开始。

以下是详细信息......

交互式控制台中的命令

#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();

然后我收到错误

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()

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 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=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\DB\InteractiveConsoleDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

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; }
    }
}

带方法的课程

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}

希望有人知道我做错了什么,可以帮助我。

非常感谢

2 个答案:

答案 0 :(得分:0)

在我终于开始工作之前,我已经挣扎了一段时间。

创建一个新的部分类(例如名为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");

粘贴后,将连接字符串中的&quot;替换为\",转到C#interactive中的行尾并按Enter键。

然后你应该可以在C#Interactive窗口中使用db,就好像它在你的应用程序中一样:

Print(db.Employees.Count());

答案 1 :(得分:0)

我意识到这很旧,但是我找到了一种无需更改代码或创建任何代理或其他解决方法的代码即可完成此工作的方法。我可以通过编辑交互式窗口本身的配置文件来完成这项工作。在这篇文章中查看我的回答:

Project can't find my EF connection string in C# Interactive

如果您的应用程序依赖于它,那么您可能还必须添加其他配置数据。对我来说,仅添加连接字符串就足够了。