I just started building a new ASP.Net Core website using EF Core. I created the template using the normal template in Visual Studio with User Authenticaion. Very basic. ApplicationDbContext and migrations is included and I successfully updated the database with the identity tables. I then add my own classes and add them to the ApplicationDbContext as DbSet. Tried with Add-Migration but the Up and Down methods are empty.. I have tried different solutions but most of them suggest to add dbset to the db context class... Which I have already done. What am I not seeing here?
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Customer> Customers { get; set; }
public DbSet<UserManual> UserManuals { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<UserManualCustomer>()
.HasKey(t => new { t.CustomerId, t.UserManualId});
builder.Entity<UserManualCustomer>()
.HasOne(pt => pt.Customer)
.WithMany(p => p.UserManualCustomer)
.HasForeignKey(pt => pt.CustomerId);
builder.Entity<UserManualCustomer>()
.HasOne(pt => pt.UserManual)
.WithMany(p => p.UserManualCustomer)
.HasForeignKey(pt => pt.UserManualId);
base.OnModelCreating(builder);
}
}
Startup.cs
public class Startup
{
public const string ConnectionString = @"Server=Server=(localdb)\\ProjectsV13;Database=FSCIDb;Trusted_Connection=true;MultipleActiveResultSets=true";
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddScoped<IRepository, Repository>();
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Ok so I solved the problem... Sort of. As an insanity check I created a new web project and added one file at a time. Worked! I think I messed up the other web project by moving around the ApplicationDbContext classe and the Data folder with the Migrations... So I will just leave it be :)