Add column to aspnetusers with database first - asp.net

I have found many tutorials for adding columns to the Identity tables (which I have successfully moved to the application database) with database migrations however my understanding is this is not applicable in database fist projects. So...how do I add columns to the aspnetusers table in a database first project?
I would like to ad a bit type column called Is RegComplete which is initially set to 0 then at some point when the user has completed some more tasks then set to 1.

OK, I've cracked it! Firstly I didn't realise that although I have moved the Identity tables to the Application database there is still two Database Contexts, one for the application tables which are DB First, and the other for the Identities tables.
I was able to enable migrations and add the column using code first and migrate then update the database. The new column is now available in the controller.
I found this tutorial which helped me: http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

The easiest solution:
Add columns into AspNetUsers table
Add properties into IdentityModels.cs class (Check attachment)
Add same properties into AccountViewModels.cs\RegisterViewModel class
Compile and it will work.
Attachment
(VS 2017, MVC5)

Related

A few questions regarding importing a manually created data entity

I used the data entity creation wizard and selected Reqplan table as the main data source, then I manually added ReqPlanVersion, ReqPO, ReqTrans table as additional data sources and created the relationships below.
As for the data entities fields I manually dragged a subsets of fields from the three manually added tables.
However when I try to import the data and add file, I receive the following issue:
Q1. In the past for some other entities I have changed ‘Allow Edit on Create’ from ‘Auto’ to ‘YES’ on these fields and it has worked but I am not sure if it’s the only way or is it following best practice? Also what is the determining factor for a field to be editable or not during import since they are all on AUTO?
When I try to map source to staging manually by drawing the mapping lines I get below issue:
Q2. What is going on with the configuration key? Is it because I manually dragged the fields from the additional data sources but not using the data entity creation wizard?
Lastly I been getting below issue:
Q3: Is there a way to find out which unique key it is referring to? Is it talking about the EntityKey in my Data Entity or Indexes in staging table? In either case there are more than one so I am not sure what it is referring to?
Thanks in advance.
Response from the community forum:
1) Check allowEdit property on table itself, so if it is "No" there then auto means "No". If you want to update them through data entity you will have to force them to "Yes"
2) It's not connected to manual addition, it just says that tables used in the entity has configuration key disabled, so you cannot export or import data into them, however, these tables could be added by wizard or manually, it does not matter. Also, Configuration key could be on fields as well or on EDT that these fields use, check them as well.
3) Entity has Key node and there and there you have key generated by wizard for you. It is used by framework to understand if record should be updated or created, if it does not work for you, change it on the data entity and regenerate staging. You need to refresh staging because error you get is SQL error, at this stage SSIS transfers data from a file into a staging table and data could not be copied because of index vialotion, so check staging table index and see if your file has any duplicates.

How can I set the schema dynamically for a Code First DbContext?

I have an EF6 Database First application that uses tables generated to an edmx from the dbo schema.
To update certain large pricing tables that work in concert with each other, I create new tables in a new schema based on the date, then inside a transaction, move the current tables to a backup schema, and move the new tables to the dbo schema. This is implemented using a new MetadataWorkspace created by reading the edmx file and changing the schema, and allows me to have two DbContexts where one works with the existing data in the dbo schema, and the other works with the new tables in the new schema. And works great for Database First!
See this SO article.
For CodeFirst, one can set the modelBuilder.HasDefaultSchema in OnModelCreating, but then the DbContext is locked down, and OnModelCreating is not called again for new DbContext instances, so whatever schema was set is now used for all such DbContexts for the duration of the application.
My question is – how can I dynamically change the DbContext with CodeFirst where I can have two DbContext, each using different schema? I cannot just define two DbContext derived classes since the schema name is dynamic.
Apparently this cannot be done but once since the DbContext is locked down and keeps the schema name. I plan to address this need by keeping the second schema name fixed rather than dynamic. Would be nice if could "clone" the locked down DbContext with a new schema name but currently not possible anyway that I have found. Closing.
You can set the the schema dynamically in EF6. You need to adjust the way you initialize your DBContext though.
I found most of what I was looking for here:
Multi-Tenant With Code First EF6

Customizing AspNet Identity Authentication DB objects

I am using Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext class for authentication of my users. This is a new concept to me and it confuses me a lot.
By default it creates 5 tables with default names: AspNetRoles, AspNetUserRoles, AspNetUserLogins, AspNetUsers which I think are a bit forcing on the user and does not suit my architecture. Also I want to add more columns to the above mentioned tables.
Is there a way to change the table names and add new columns to the existing tables?
using VS 2013, SQL Server 2014 Express.
By default the class for the user that you get from the project template is called something like ApplicationUser. Just adding new properties to that one let you store new columns.
Here is a question that explains how to change the table names: How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

To fill up asp.net membership

I believe I want to do a update query.
I've found a tutorial that introduces me to ASP.Net membership where I can use an Access provider to fill my database needs. However, I have an existing Access database of customers, but I do not know how to copy this existing database into the new ASPNetDB.mdb. It's identical to the SQL provider as far as I know.
Every table is related to each other so I cannot enter new records. I've wanted to append the UserName column, the passwords, email's, and a new field to the new ASPNetDB.mdb. How would I go about doing this?
So instead of having to copy records from one database to another database why don't you just create a custom membership provider and use the same existing database.
Basically you create a class that inherits from the MembershipProvider class and you implement all the methods from the MembershipProvider class. I.E. ChangePassword, CreateUser, DeleteUser, etc... In your implementation you write custom code to access the tables that already exist.
This way you can keep your current database and still use ASP.NET membership.
Here is an MSDN article with a sample implementation.
Sample Membership Implementation
As suggested, if the table structures ARE the same, then why not just make a copy of the original database and re-name to the same as your ASPNetDB.mdb.
If you're looking to import older data, then again simple open ASPNetDB.mdb with Access and import the data + tables from the older database. Access can and WILL import the tables and WILL ALSO BRING IN related data tables. This will create new local tables, but then again you stating they are the same. (so delete the existing tables in ASPNetDB and import the other ones).
You can ALSO import the table data into existing tables if they are empty. So assuming empty tables, then a compact + repair will re-set auto numbers, and then you can link to the older database and use append quires. I would only do this if a few tables. You have to import the "parent" tables first if referential integrity is enforced as it makes little sense to add child records first!
However I fail to see why not just making a whole copy of the older existing database and re-naming it as ASPNetDB would not work if your claim of the tables being the same in both.

What is aspnet_Users and aspnet_Membership? Which one should I use? What is the difference?

I'm using default membership class in my website. But I'm a little confused with the complex database design of the ASPNETDB.
Let's say I want to add details about user like name, surname, address etc... Where should I put them aspnet_Membership table or aspnet_Users table and why? I mean I can see that I should put the details on aspnet_Membership. But then again why there is two of them and the bindings on aspnet_Users...
Or lets say I'm adding a messaging into that which table should I key to, for the user details?
I'm confused and I would be appreciated if you can tell me the use of these two tables.
Here is the diagram for the default ASPNETDB which you should probably have:
Rather than modifying the built in ASP.NET membership schema, you can use ASP.NET profiles to store custom properties about your users. Here's one tutorial on the subject.
You can add the profile tables to your schema using aspnet_regsql.exe (which you're probably already familiar with, given that you have already generated most of the schema). Use the -R p switch to get the profile tables/procs.
If you want to connect your custom table to ASP.NET membership, use UserId as foreign key from aspnet_Users table rather than aspnet_Membership table to your custom table. For reference, please note how aspnet_Profile or even aspnet_Membership is using UserId as foreign key from aspnet_Users table.
Please see the database diagram here:
http://msdn.microsoft.com/en-us/library/Aa478948.asp2prvdr0102l(l=en-us).gif
Good luck ...
I would advise not to alter any of the tables provided by the ASP.NET membership schema. To store additional data use the already mentioned ASP.NET profiles option or create a custom one which maybe fits your bill beter.
The reason why there are two tables for storing user data is already answered over here.
Good luck which choosing the right solution.

Resources