Solution explorer image and package manager console image -
I am trying to add migration by giving command like this:
Add-Migration addingtable -Context SampleApplicationContext" it is throwing errorUnable to create an object of type 'SampleApplicationContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. mypackagemanagerconsole
My startup project is Sample.Api and default project is datarepository\sampleapplicationdatabase
My SampleApplicationContext is in class library
Please help someone to get of this issue, thanks in advance
my SampleApplicationContext.cs code
-----------------------------------
namespace SampleApplicationDatabase
{
public class SampleApplicationContext:DbContext
{
public SampleApplicationContext(DbContextOptions<SampleApplicationContext> options) : base(options)
{
#if RELEASE
this.Database.Migrate();
#endif
}
public DbSet<AdminCredentials> AdminCredentials { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
}
}
my sample.api appsetting code
-----------------------------
{
"SampleApplicationConnectionstring": {
"ConnectionString": "Server=TEKFRIDAY281;Database=SampleApplication;User ID=sa;Password=friday123!;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=false"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
my sample.api startup code
--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API_Interface;
using DataAccessLayer;
using IRepository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Repository;
using SampleApplicationDatabase;
namespace Sample.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IData, Data>();
services.AddTransient<IUnitOfWork, UnitOfWork>();
services.AddControllers();
services.AddMvc();
services.AddDbContext<SampleApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
try to fix you appsettings json
and startup