Getting EF Core 2 Scaffolding to use plurialization correctly - ef-core-2.0

New to EF Core 2, used EF 6.x with Database first.
I was hoping that when I scaffolded a database with tables that had pleural names (ie Users) the class created for each table would be singular (ie User). Instead, I get something like public virtual DbSet Users Users { get; set;}. What I was hoping for was public virtual DbSet User Users { get; set;}.
Is there a switch on the Package Manager Console line that will allow me to do this or must I edit after scaffolding?
M

EF Core doesn't include pluralization, but it can be added as a design-time service (IPluralizer).
The EF Core Power Tools include an implementation.
If you're using EF Core 2.1.0-preview2-final, you can use my package:
Install-Package Bricelam.EntityFrameworkCore.Pluralizer -Version 1.0.0-rc2
Scaffold-DbContext ...

Related

Add column to created identity table in asp.net mvc project?

I use identity 2.0 in my MVC 5 project.
First time when I run the project in my DB has been created all default tables for authentication and authorization:
In AspNetUsers table I need to create additional column named LoyoutId of type integer.
My question is how can I add column to created default tables?
You can add profile data for the user by adding more properties to your ApplicationUser class, please visit this to learn more.
In your case:
public class ApplicationUser : IdentityUser
{
public int LoyoutId { get; set; }
}
Then open the Package Manager Console and execute following commands:
PM> Enable-migrations //You don't need this as you have already done it
PM> Add-migration Give_it_a_name //This will generate a database script file
PM> Update-database //Run script file against database.
Now, all the new properties will turn into AspNetUsers table.

ASP.NET Identity 2.0 DB First Approach: Adding new Columns

I am trying to use ASP.NET Identity 2.0 with existing database.
I have created an MVC project (which uses Individual Account Authentication), then I registered for a use in order to create the DB.
Then:
I created scripts for the necessary tables and added them to my own DB
I added ADO.NET Entity Data Model (database first) which include my tables plus identity tables.
I ran the application and registered for a user, everything is going fine.
Now, I need to add a relation to AspNetUser table.
I added the Column LocationId with the relationship in DB.
I Added the following to the Application User Class:
public virtual Region Region { get; set; }
Then, I updated my Model and run the application, when I tried to register for new user, I got the following error:
AspNet UserLogin: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNet UserLogin' that has no keys defined.
How is it possible to continue using DB First Approach in this scenario?
Identity uses Code First approach for making Identity System make customization as more as possible and you are using DB first approach for your common data access. So there are 2 contexts, one is for your data and other is for you Identity. You need to write Identity classes and make a code first migration of Identity context class by typing in the Package Manager Console as:
`Enable-Migrations -ContextNameType [Your_Identity_Context]
This will enable code first migrations just for your Identity context type. If you want to add region property in your user table, then in 'ApplicationUser' (or any class derived from IdentityUser) add the required region property and then apply the migrations to update the user table in the database.
Generating SQL script and applying to the database is not a good approach.

How can I create and use views using EF6 Code First?

Is there actually any official strategy for creating and using views with EF6.1 Code First? I can reverse the database into Code First and get tables not views. I can import the database into Model First and get views and then do Generate DB from Model and my edmx is modified and the views are converted to tables. MSFT seems to have all but abandoned Model First, and the new Update 2 seems to have reverse engineering for Code First so I feel forced to convert to Code First but has the EF team given support for any reasonable approach to using views or stored Procedures in Code First? After all CF is supposed to create the DB but what - are you no longer supposed to use either of these SQL Server features in a .NET EF application?
For starters you can use views and stored procedures that are already created in the database. For views you don't have to do any mapping if you create a code like this:
public TestContext : DbContext
{
public DbSet<User> AdminUsers { get; set; }
}
And you have a view in the database named dbo.AdminUsers and it can be mapped to the class User (contains all required properties with the same name as the property).
For stored procedures you can use the SqlQuery function either through the Database property of the DbContext such as:
var userActivityReport = context.Database.SqlQuery<UserActivityReport>(
"dbo.GetUserActivityReport #p0, #p1", startDate, endDate);
Or through the SqlQuery function of the DbSet class:
var newUsers = context.Users.SqlQuery("dbo.GetNewUsers #p0", count);
If you want to create, modify or delete views and stored procedures via Entity Framework you can either use custom migration operations see http://dolinkamark.wordpress.com/2014/05/03/creating-a-custom-migration-operation-in-entity-framework/
For event better integration you can use the Public mapping API with a library provided by a community member: https://codefirstfunctions.codeplex.com/

You custom database IdentityDbContext

I was trying to develop an application and created my own database which then was reflected to an entity model using database first. So I now already have let's say myOwnDBContext.
Then I thaught it wuld be a nice idea to mix it with the classes generated by IdentityDbContext. But when i just changed the connection string in IdentityModel like this
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("myOwnDBConnectionString")
{
}
}
The tables were not generated as I would expect in myOwnDB but instead I received the following error.
The entity type ApplicationUser is not part of the model for the
current context.
Is there a way I can mix the defualt database of IdentityDbContext with my own databse?
I'm not sure if this can work without using EF migrations, but I do have it working with migrations. Specifically, two DbContext derived classes (one for custom entities, one derived from IdentityDbContext).
For the migrations I had to enable-migrations from the package manager console for both contexts and use the -MigrationsDirectory parameter to place the two sets of migrations into distinct folders (a new feature for EF6).
Then when running update-database the tables are created properly for each context. The update-database command will need a -ConfigurationTypeName parameter to specify the exact migration to use.

Error:'Unsupported context type' while creating a new controller

I am going to implement MvcMusicStore using ASP.NET MVC3, Linq to Sql class instead of Entity Framework, MS SQL Server 2008 pro instead of express ed.
I got the tutorial from mvcmusicstore.codeplex.com
I used Linq to Sql class and the Datacontext is MvcMusicSrotedataContext. When i try to create a new class using this
it shows an error in a new window when i click add button Error:'Unsupported context Type'
So, could you please help me to solve this?
Thank You.
The built-in MVC scaffolding doesn't support Linq to SQL -- you'll have to use Entity Framework instead. (Or don't use the scaffolding, build your own controller/action logic manually. Or use a scaffolding plugin that supports Linq to SQL.)
I got the same issue with EF. I am using the VS 2012.
Background:
The reason for my case was.. this auto generation process (Scaffolding) seems not recognize the partial class concept.
I used the model first approach and I have used inheritance with the entities.
Ex: entity “B” and “C” is inherited from “A”
So in my generated model class “DataModelContainer” which is inherited from “DbContext”,
There is no definition for “DbSet” and “DbSet”
i.e: the following two lines were not there
public DbSet<B> B { get; set; }
public DbSet<C> C { get; set; }
Generated “DataModelContainer” class I a partial class, so I completed the other part, using the concept of partial class. And this would be a problem for Scaffolding.
Solution
My workaround was, just removed the partial class I added manually. And added the definitions for “DbSet” and “DbSet” in to the auto generated class.
The problem with this solution is, I have to repeat the same thing when I regenerate the model classes.

Resources