C# 为什么不能用=运算符设置DbContextOptions?

C# 为什么不能用=运算符设置DbContextOptions?,c#,entity-framework-core,C#,Entity Framework Core,为什么我不能使用以下命令 IServiceCollection service= new ServiceCollection(); var opt = new DbContextOptionsBuilder<Application>().UseSqlite("Data Source=MyDatabase.db"); service.AddDbContextPool<ApplicationContext>(options => options = opt); ISe

为什么我不能使用以下命令

IServiceCollection service= new ServiceCollection();
var opt = new DbContextOptionsBuilder<Application>().UseSqlite("Data Source=MyDatabase.db");
service.AddDbContextPool<ApplicationContext>(options => options = opt);
IServiceCollection服务=newServiceCollection();
var opt=new DbContextOptionsBuilder().UseSqlite(“数据源=MyDatabase.db”);
AddDbContextPool(options=>options=opt);
而不是下面

IServiceCollection service= new ServiceCollection();
service.AddDbContextPool<ApplicationContext>(options => options.UseSqlite("Data Source=MyDatabase.db"));
IServiceCollection服务=newServiceCollection();
AddDbContextPool(options=>options.UseSqlite(“数据源=MyDatabase.db”);
编辑: 错误消息:

System.InvalidOperationException
  HResult=0x80131509
  Message=No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetRelationalService[TService](IInfrastructure`1 databaseFacade)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
....
System.invalidoOperationException
HResult=0x80131509
Message=尚未为此DbContext配置任何数据库提供程序。可以通过重写DbContext.OnConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,那么还要确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。
Source=Microsoft.EntityFrameworkCore
堆栈跟踪:
在Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider、IDbContextOptions contextOptions、DbContext context)
在Microsoft.EntityFrameworkCore.DbContext.get\u InternalServiceProvider()中
在Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.get_Instance()中
在Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IIinfrastructure.get_Instance()中
位于Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetRelationalService[TService](IInfrastructure`1 databaseFacade)
位于Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade DatabaseFacade)
....

这是因为AddDbContextPool接受操作作为参数。此操作依次接受DbContextOptionsBuilder类型的对象。框架将在某个时候调用此操作,框架将向此操作传递它使用一些内部规则和技巧创建的生成器实例。您应该进一步处理此对象,而不是重新定义它

如果您进行了赋值,您将有效地用自己的对象覆盖框架创建的对象,而框架对此一无所知。因此,对象创建、内部管道等的所有内部逻辑都丢失了

这里,
(options=>options=opt)
的工作原理与常规方法类似:

private void Method(TypeOfParameter options)
{
    options = opt;
}

这里,
options
变量被覆盖。在方法之外不会发生任何事情。用于调用该方法的变量未更改。这就是为什么作业是无用的,没有任何效果。

下面的代码大致模拟了被接受的答案的含义

using System;


class DbContextOptionBuilder
{
    public string ConnectionString { get; set; }
    public override string ToString() => $"ConnectionString: {ConnectionString}";
}

class ServiceCollection
{
    public void AddDbContext(Action<DbContextOptionBuilder> job)
    {
        DbContextOptionBuilder o = new DbContextOptionBuilder { ConnectionString = "Empty" };
        job?.Invoke(o);
        Console.WriteLine(o);
    }
}

class Program
{
    static void Wrong(DbContextOptionBuilder o)
    {
        o = new DbContextOptionBuilder { ConnectionString = "SQLITE" };
    }

    static void Correct(DbContextOptionBuilder o)
    {
        o.ConnectionString = "SQLITE";
    }

    static void Main(string[] args)
    {
        ServiceCollection services = new ServiceCollection();
        services.AddDbContext(Wrong);
        services.AddDbContext(Correct);
    }
}
使用系统;
类DbContextOptionBuilder
{
公共字符串连接字符串{get;set;}
公共重写字符串ToString()=>$“ConnectionString:{ConnectionString}”;
}
类服务集合
{
public void AddDbContext(操作作业)
{
DbContextOptionBuilder o=新的DbContextOptionBuilder{ConnectionString=“Empty”};
作业?.Invoke(o);
控制台写入线(o);
}
}
班级计划
{
静态无效错误(DbContextOptionBuilder o)
{
o=新的DbContextOptionBuilder{ConnectionString=“SQLITE”};
}
静态无效正确(DbContextOptionBuilder o)
{
o、 ConnectionString=“SQLITE”;
}
静态void Main(字符串[]参数)
{
ServiceCollection服务=新建ServiceCollection();
services.AddDbContext(错误);
services.AddDbContext(正确);
}
}

问题出在哪里?这不管用吗?它甚至可以编译吗?我猜lambda的
options
参数是一个副本,而不是一个引用
private void方法(TypeOfParameter options){options=options.dosomethingthattchangesoriginalstate();}