我正在使用实体框架和SQL来处理数据库。
我的上下文类是:
public class AnimalBreedsContext : DbContext
{
public DbSet<AnimalBreed> AnimalBreeds { get; set; }
public AnimalBreedsContext(DbContextOptions<AnimalBreedsContext> options) : base (options)
{
}
}
类 AnimalBreeds 是一个具有一些字符串属性(如 Name 等(的类。
现在我想向Autofac注册AnimalBreedsContext类。我尝试了类似的东西:
class ContainerConfig
{
public static IContainer Configure()
{
var builder = new ContainerBuilder();
var options = new DbContextOptionsBuilder<AnimalBreedsContext>()
.UseSqlServer("configString")
.Options;
builder.RegisterType<AnimalBreedsContext>().WithParameter(options).AsSelf();
return builder.Build();
}
}
但问题是"参数选项无法从AnimalBreedsContext转换为Autofac.Parameter"。
还有其他选项可以让我向Autofac注册此类吗?
看起来您没有为WithParameter
提供正确的参数。 这是显示所有重载的 Autofac 文档。
如果不将参数包装在 AutofacParameter
对象之一中,看起来您可以将参数的字符串名称作为第一个参数,然后将实际参数对象作为第二个参数:
builder.RegisterType<AnimalBreedsContext>()
.AsSelf()
.WithParameter("options", options);