【问题标题】:C# Interactive and Entity FrameworkC# 交互和实体框架
【发布时间】:2018-02-24 14:11:50
【问题描述】:

我正在尝试在 C# Interactive 中运行一个方法,该方法使用实体框架从本地数据库返回一些数据。但它返回一个错误,指出可以在应用程序配置文件中找到名为“InteractiveConsoleDBEntities”的连接字符串。 我首先使用数据库。 我使用“Initialize Interactive with project”选项从 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}

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

非常感谢

【问题讨论】:

    标签: entity-framework c#-interactive


    【解决方案1】:

    在我终于让它工作之前,我在这个问题上挣扎了一段时间。

    为您的构造函数创建一个新的部分类(例如名为 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 替换为您的连接字符串,但不要按 Enter:

    var db = new YourNamespace.Models.YourEntities("ConnStringHere");
    

    粘贴后,将连接字符串中的 &amp;quot; 替换为 \" ,在 C# 交互中转到行尾并回车。

    那么您应该能够在您的 C# 交互式窗口中使用 db,就好像它在您的应用程序中一样:

    Print(db.Employees.Count());
    

    【讨论】:

      【解决方案2】:

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

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

      如果您的应用依赖它,您可能还需要添加其他配置数据。对我来说,只需添加连接字符串就足够了。

      【讨论】:

        猜你喜欢
        • 2014-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-26
        相关资源
        最近更新 更多