ASP.NET Core RC2 Identity Database Schema Migrations - c#

In the new ASP.NET Core RC2 template (With no database available at the back-end), when I try to register a user it throws the following error for adding Migrations.
While in the past, it use to generate the database for us without adding initial migration when we run the application. Is there a way to create the database in ASP.NET Core RC2 without Initial Migration?

For testing scenario you can use context.Database.EnsureCreatedAsync() somewhere during startup. Ensure you are using this pattern (for now) and not inject it directly in configure, to avoid issues during startup.
For future reference the code section copied from the MusicStore example application.
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var db = serviceScope.ServiceProvider.GetService<MusicStoreContext>();
if (await db.Database.EnsureCreatedAsync())
{
await InsertTestData(serviceProvider);
if (createUsers)
{
await CreateAdminUser(serviceProvider);
}
}
}
because at this point, there is no scope yet. If you later want to apply the migrations you can use context.Database.AsRelational().ApplyMigrations() (GitHub issue)

Related

What is better to use when testing a web application that uses EF Core, 'Mock', or 'InMemory'?

There is a web application (.NET Core 3.1) that uses a MySql database using EF Core. There is a class in the application (ControlDataBase) that is responsible for executing the request via EF Core and returns data to the main method (ConnectUser). As a result, when I wanted to test the main method (ConnectUser), I thought that I should write Mock for ORM, but in sites said that this makes no sense and it is better to use InMemory at all. As a result, who better knows how to write Mock EF Core or will it be enough to use ImMemory for testing?
Example:
ActionResul ConnectUser(){
var idUser = ControlDataBase.GetUserId(dataUser.Login);
}
string GetUserId(string login) {
return db.Users.FirstOrDefaultAsync(user => user.Login.Equals(login));
}

Associate data to default ASP.NET UAM user using Entity Framework Core

Trying to create a simple MVC application that has basic user account management with social account sign-up.
Created ASP.NET Core 2.2 app with individual authentication, added EF core via NuGet (sqlserver and tools). I have built some basic ASP.NET MVC apps and MVC apps in other languages previously.
I got Facebook login to work per this tutorial.
Now I'm lost.
There's a "Data" directory with a bunch of code in it already, and the normal MVC directories. However, I don't see the MVC files that would correlate to any of the pages or actions associated with the account management I can clearly access when I launch the application, including /Identity/Account/Login, /Identity/Account/Manage, /Identity/Account/Manage/SetPassword, etc.
While having all of this functionality up and running in minutes is cool, already I'm at a disadvantage of not knowing how or why it works. Searching for anything I would expect to shed some light on this gives me technical documentation that makes my head spin and provides zero enlightenment.
I want to have a collection of data sets ("Books") associated to each user. One User to many Books. Given that the User (account?) model is not in the Models directory of the MVC application I don't think that's where it's supposed to be added. And if it is, I don't know how to create that association with the User.
This is what I'm seeing in a file called ApplicationDbContextModelSnapshot.cs
namespace Bookshelf.Data.Migrations
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
...
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex")
.HasFilter("[NormalizedName] IS NOT NULL");
b.ToTable("AspNetRoles");
});
... and so on, which looks fairly readable, but does not match up to any of the tutorials I've seen so far.
TL;DR:
Where/how do I correctly create a new models? Is it like this in the Models directory (how I'm used to) or is this obsolete?
Either way, how do I correctly associate data with a user given the baked-in user account management for ASP.NET Core 2.2?
There's a "Data" directory with a bunch of code in it already, and the normal MVC directories. However, I don't see the MVC files that would correlate to any of the pages or actions associated with the account management I can clearly access when I launch the application, including /Identity/Account/Login, /Identity/Account/Manage, /Identity/Account/Manage/SetPassword, etc.
This is because from ASP.NET Core 2.1 Identity is being provided as Razor Class Library with the ASP.NET Core project templates. If you want to see those Identity related codes and customize then you have to Scaffold Identity in your project.
Here is more details about this: Scaffold Identity in ASP.NET Core projects
Moreover if you need ASP.NET Core Identity in MVC format then here is my GitHub Repository where Razor Page Identity has been converted to MVC in ASP.NET Core 2.2.

Glimpse EF6 SQL Tab disabled / not Capturing Data Access

I have used Glimpse before and really liked it so when this new project came up I went right to it. This time however I am using the EF plugin which is new to me. Further the project is broken up into different layers. Overall glimpse is working and it is just the SQL tab that is not active. Logging is turned on with no errors.
My Solution is like so:
VS 2015
Repository Project EF 6.0.0.0
Glimpse ADO 1.7.3.0
Glimpse Core 1.8.6.0
MVC & WebAPI Project MVC 5.2.3 WebApi
Glimpse ADO 1.7.3.0
Glimpse ASP.Net
Glimpse Core 1.8.6.0
Glimpse EF6 1.6.5
Glimpse MVC 5
So I am creating a manual SQL connection and most likely that is where my mistake is?
In the repository I establish my context like this:
public class PROJDataContext : DbContext, IPROJDataContext
{
public PROJDataContext() : base(PROJConnection, true)
{ }
private static GlimpseDbConnection PROJConnection
{
get
{
//glimpse wrapper
var conxSection = ConfigurationManager.ConnectionStrings["PROJData"];
return new GlimpseDbConnection(new SqlConnection(conxSection.ConnectionString));
}
}
When I fire up the project localhost the SQL tab is disabled. I am assuming this is because the db has not been accessed yet.
I browse to a page that passes request to WebApi. WebApi queries the repository. Data is retrieved from repository and sent back to WebApi which then passes it back to the MVC controller action which then returns a view showing the data.
Glimpse accurately tracks ALL of this except the db portion so any pointers would be greatly appreciated.
Update...when I wrote this up it was EOD Friday. Talking this through with a colleague today I think I realized the problem. My MVC app which has the Glimpse hooks does NOT call SQL instead it calls off to web api and web api calls the repository which has the Glimpse SQL hooks.

Using log4net to log in Api from class library

I have a webapi for which I am trying to debug.
I use a generic repository structure so all my entity framework calls are made in a separate class library.
So my web Api post end point effectively just calls service.insert(entity). Where the generic insert is in the separate class library.
Logging is currently setup and working in the api. Now I want to log the insert Sql generated by entity framework in the parent applications text log file (as something strange is going on in the live environment)
How would I go about doing this please?
How to do this depends on what version of Entity Frameework you are using. In EF6 and later it is simple:
using (var context = new DataContext())
{
// log is a log4net logger
context.Database.Log = message => log.Debug(message);
// insert the entity
}
See this blog series for more information - part 3 shows an example of logging to NLog with a command interceptor - and this page for options relating to earlier versions of EF.

How to get OWIN Database Context in Database First Project

I've worked for days getting Identity Framework to work on my database first project. The book that I have appears only to explain how to implement it using database first, but it mentioned OWIN.
I can see from the code first version that we can get an OWIN instance of a database context by calling a static Create method on the database context that returns an instance of itself.
Isn't OWIN compatible with database first models and database contexts? If so, how do I get an instance of my database context using OWIN?
This is how you do it within code first:
Database Context
public class AppIdentityDbContext : IdentityDbContext<AppUser> {
public AppIdentityDbContext() : base("IdentityDb") {}
public static Create() {
return new AppIdentityDbContext();
}
}
Calling from OWIN start class:
app.CreatePerOwinContext<AppIdentityDbContext>(AppIdentityDbContext.Create);
I realised that the OWIN stuff isn't directly related to whether your application uses Entity Framework code first or database first.
The OWIN context is used to get to the UserManager, RoleManager etc. for ASP.NET Identity Framework. The DbContext specified in this context is really for use solely by these classes and not just for general use within your ASP.NET MVC application.
I'm now using a IoC Container to inject my DbContext into my controllers within my projects. (I'm using Ninject, but there are others such as Unity, MEF)

Categories

Resources