Невозможно создать объект типа MyContext. Для различных шаблонов, поддерживаемых во время разработки ⇐ C#
-
Anonymous
Невозможно создать объект типа MyContext. Для различных шаблонов, поддерживаемых во время разработки
I have ConsoleApplication on .NET Core and also I added my DbContext to dependencies, but howewer I have an error:
Unable to create an object of type 'MyContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
I've added: var context = host.Services.GetRequiredService(); Also I've added private readonly DbContextOptions _opts; in my Post Class:
using (MyContext db = new MyContext(_opts)) { db.Posts.Add(postData); db.SaveChanges(); } This how I added service:
.ConfigureServices((context, services) => { services.Configure(opts => context.Configuration.GetSection(nameof(DataOptions)).Bind(opts) ); services.AddDbContext((provider, builder) => builder.UseSqlite(provider.GetRequiredService().Value.ConnectionString) ); }); And this is my Context:
public sealed class MyContext : DbContext { private readonly DbContextOptions _options; public DbSet Posts { get; set; } public DbSet Votes { get; set; } public MyContext(DbContextOptions options) : base(options) { _options = options; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlite("ConnectionString"); } } } I tried add-migration and has this error
What I do wrong?
Источник: https://stackoverflow.com/questions/577 ... erns-suppo
I have ConsoleApplication on .NET Core and also I added my DbContext to dependencies, but howewer I have an error:
Unable to create an object of type 'MyContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
I've added: var context = host.Services.GetRequiredService(); Also I've added private readonly DbContextOptions _opts; in my Post Class:
using (MyContext db = new MyContext(_opts)) { db.Posts.Add(postData); db.SaveChanges(); } This how I added service:
.ConfigureServices((context, services) => { services.Configure(opts => context.Configuration.GetSection(nameof(DataOptions)).Bind(opts) ); services.AddDbContext((provider, builder) => builder.UseSqlite(provider.GetRequiredService().Value.ConnectionString) ); }); And this is my Context:
public sealed class MyContext : DbContext { private readonly DbContextOptions _options; public DbSet Posts { get; set; } public DbSet Votes { get; set; } public MyContext(DbContextOptions options) : base(options) { _options = options; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlite("ConnectionString"); } } } I tried add-migration and has this error
What I do wrong?
Источник: https://stackoverflow.com/questions/577 ... erns-suppo
Мобильная версия