I've setup a .net core api app to host JWT token authentication using an asp identity database. That part works great. My problem is when I go to add custom classes, every migration I perform is trying to create the AspNet* tables again even though they exist. The migration is aware of my custom class (Test) and wants to create it, but the script it's trying to run dies on the AspNetRoles table (already exists error).
I create a new migration:
Add-Migration NewMigration
Then update:
Update-Database
Here is my ApplicationDBContext.cs:
public class ApplicationDBContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
{
}
// This is my custom class
public DbSet<Test> Test { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
I'm at a loss. I just need to do migrations and have EF Core ignore the AspNet identity tables.
Edit: Here is ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
}
Migrations folder:
Related
Currently, I have EF working perfectly fine with asp net core identity. My web app allows users to login in 2 ways: using their local account and using their azure active directory account. However, only the allowed azure directory accounts are allowed to log in. Therefore, I need to create another table to hold those accounts to cross check when they log in.
If you want to create a new table for the Identity project, I suggest you could try to modify the identity EF's dbcontext with the new create model class.
More details, you could refer to below codes:
public class UserRelationship
{
public int UserId { get; set; }
}
Modify the ApplicationDbContext :
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<UserRelationship> UserRelationships { get; set; }
}
Then you could enable the migration and update the database like this article shows.
I have created a business layer and data layer for a custom task management system. It just has 2 related tables Tasks and TaskNotes. Now, I would like to build an MVC application on top of it using the Individual User Accounts that you can specify for authentication (ASP.NET Identity).
What is the easiest way to get my tables and the identity tables into one database?
The way to do this is to have one DbContext for all your entities.
You should add Task and TaskNote to your ApplicationDbContext which inherited from IdentityDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Tasks> Tasks { set; get; }
public DbSet<TaskNotes> TaskNotes { set; get; }
}
Since I already have a DB project in my solution, I was wondering can I leverage Entity Framework so that the Web Application may communicate with the database without having to do any migrations, or is migration necessary in order to use the Context of Entity Framework?
If migrating is not necessary then I also won't need the designer (edmx) file, I could just make my models and link them into my context right?
Not necessary at all.
I often write short solutions towards some databases where I just want to add some functionality on the fly. What I typically do is to just open SQL Server Object Explorer in VS2015, Expand the table I'm interested in, and just write a class on the fly, matching the fields that I need.
I.e for a table named "Classes" in the database, I would end up with a class in C#
public class SomeClass
{
public int Id {get; set;}
public string Title {get; set;}
// +other intersting fields
}
Then I simply create a DbContext class pointing to my database:
public class MyContext : DbContext
{
public DbSet<SomeClass> Classes{get; set;}
public MyContext() : base("myConnectionString"){}
}
That's it. No need for migrations. It then simply just works :)
I wonder if anyone out there can help me with the following problem...
I have a created a EF code first database (sqlexpress) that uses Identity v2 -I can see within that database all my models, including the Identity related tables starting with Identity* (ie. IdentityUserClaims, IdentityUserLogins, IdentityUserRoles IdentityRoles & IdentityUsers).
I then create a brand new MVC project as well as adding the same connection string. When I register a new user -for some reason it goes off and creates Identity v2 tables starting with AspNet* (ie. AspNetUserClaims, AspNetUserLogins, AspNetUserRoles AspNetRoles & AspNetUsers).
[I am using... EF 6.1.1, Identity 2.1, MVC 5.2]
Why does the Identity naming schema differ between EF and AspNet ? how do I make MVC use the EF schema Identity* ?
You can override table names by [Table("MyUsers")] attribute on top of your classes:
[Table("WhateverILikeUsers")]
public class ApplicationUser : IdentityUser
{
}
Or you can rename tables in DbContext:
public class MyContext : IdentityDbContext<ApplicationUser>
{
// other stuff
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>().ToTable("HelloIdentity");
}
}
I have a console application that is using code first Entity Framework 4.3.1. I created a class, a DbContext, a DbSet, and I have a database connection.
The issue is that I mispelled the table name and the program threw an error. I changed the name in the DbSet and the system keeps throwing the same error that has the old name.
Example:
public DbSet<SHIPPER> SHIPPERs { get; set; }
This could not find the SHIPPERs table in SQL server. No problem. I changed it to
public DbSet<SHIPPER> SHIPPER { get; set; }
and I get Invalid object name 'dbo.SHIPPERs'."
I did a search in Visual Studio for SHIPPERs and nothing was returned. What am I missing?
Note: I created another DbSet for a different table and that works.
The problem was with pluralization. Add the following code inside of your DbContext class to fix the issue:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}