C# 如何正确实例化具有构造函数的上下文
我有个问题。我有一个类,如下所示:我无法将数据保存到数据库中C# 如何正确实例化具有构造函数的上下文,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我有个问题。我有一个类,如下所示:我无法将数据保存到数据库中 public class SomeClassContext : DbContext { private IConfigurationRoot _configurationRoot; public SomeClassContext(DbContextOptions<SomeClassContext> contextOptions, IConfigurationRoot configurationRoot)
public class SomeClassContext : DbContext
{
private IConfigurationRoot _configurationRoot;
public SomeClassContext(DbContextOptions<SomeClassContext> contextOptions, IConfigurationRoot configurationRoot)
{
this._configurationRoot = configurationRoot;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(_configurationRoot["Data:ConnectionString"]);
}
当我试图实例化SomeClassContext并为构造函数提供参数时,得到的是空值
public class MyClass
{
private SomeClassContext context;
private string _categoryName;
private Func<string, LogLevel, bool> _filter;
public MyClass(string categoryName, Func<string, LogLevel, bool> filter)
{
_categoryName = categoryName;
_filter = filter;
context = new SomeClassContext(//parameters here are required. What should be my parameters here that will not result to null)
}
public void SomeMethod()
{
var obj = new MyEntity()
{
//assume I have initialize object here
};
try
{
context.MyEntity.Add(obj);
context.SaveChanges();
}
catch(Exception e)
{
//debugger throws here
//cannot save to DB
}
}
}
公共类MyClass
{
私人语境;
私有字符串_categoryName;
私有函数过滤器;
公共MyClass(字符串categoryName,Func筛选器)
{
_categoryName=categoryName;
_过滤器=过滤器;
context=new SomeClassContext(//此处的参数是必需的。此处我的哪些参数不会导致null)
}
公共方法()
{
var obj=new MyEntity()
{
//假设这里有初始化对象
};
尝试
{
context.MyEntity.Add(obj);
SaveChanges();
}
捕获(例外e)
{
//调试器在这里抛出
//无法保存到数据库
}
}
}
您能否编辑您的问题,以便在第三个代码块中包含MyClass
构造函数和SomeClassContext
构造函数的实际参数?当您使用ASP.NET Core中的依赖项注入容器时,您不会使用new
关键字。如果您无法更改构造函数,则可以使用IServiceLocator
接口,该接口允许您在正确配置服务的情况下定位服务。此外,您通常不会将IConfigurationRoot
传递给上下文,以便它可以获取连接字符串,通过已从调用的UseSqlServer
函数传递DbContextOptionsBuilder
startup@urig我编辑了我的问题。Camilo我会研究iSeries Vicelocator谢谢你编辑你的问题,在你的第三个代码块中包括MyClass
构造函数和SomeClassContext
构造函数的实际参数?当您使用ASP.NET Core中的依赖项注入容器时,您不会使用new
关键字。如果您无法更改构造函数,则可以使用IServiceLocator
接口,该接口允许您在正确配置服务的情况下定位服务。此外,您通常不会将IConfigurationRoot
传递给上下文,以便它可以获取连接字符串,通过已从调用的UseSqlServer
函数传递DbContextOptionsBuilder
startup@urig我编辑了我的问题。卡米洛,我会研究伊瑟维切洛卡特的谢谢
public class MyClass
{
private SomeClassContext context;
private string _categoryName;
private Func<string, LogLevel, bool> _filter;
public MyClass(string categoryName, Func<string, LogLevel, bool> filter)
{
_categoryName = categoryName;
_filter = filter;
context = new SomeClassContext(//parameters here are required. What should be my parameters here that will not result to null)
}
public void SomeMethod()
{
var obj = new MyEntity()
{
//assume I have initialize object here
};
try
{
context.MyEntity.Add(obj);
context.SaveChanges();
}
catch(Exception e)
{
//debugger throws here
//cannot save to DB
}
}
}