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?
Related
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)
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.
I have a Membership form in asp.net Which contains custom fields.so by that custom Field User can generate more than one field dynamically
for Example (User can add Mobile number column in their membership form (they can add what ever they need)).So i cant able to maintain static table for Membership control in ms sql.
Is there any way to create dynamic table in ms sql for Dynamic Membership form.(whenever user adding column dynamcally i should get those column in ms sql dynamic table)
please help me to solve my problem
I am fairly new to working with ASP controls. I am having some issues with a project. This project is fairly large and my team has placed the ADO.NET emtity inside the same solution but placed into a different project inside Visual Studio 2010. The original db was built through SQL Server 2008. I was wondering if it would be possible to connect a GridView control to this entity in order to show certain aspects of it?
Also, If not possible, we have a class Customer which has certain members (name, dob, ssn,...etc) and functions that will pull these customers from the db through the ADO.NET. Could I populate the GridView with a a list of instances of this class?
something like
List<Customers> CustList = new List<Customers>();
....populate the list.....
Gridview.DataSource = CustList;
Gridbiew.DataBind();
Sure you can! Just add a reference the other project from the one that has the GridView, and set the DataSource property of the GridView to the entity that is in the other project. Indeed, this is a common design in applications, to separate them by "layers"(also criticised a lot).
Hope it helps,
I want to create a relationship between a custom table (Websites) and the default aspnet tables related to Users.
I'm using code-first so for most FK relationships I would just do
public ModelName ModelName { get; set; }
With this, EF will automatically create the FK relationships. Very easy.
What's confusing is the most effective way to hook into the aspnet users/membership table. Do I create a new model Users that acts as an interface so that I can implement custom user code?
Is there a best way to do this that fits well into EF best practices? I basically just want to relate a user to the Websites table/model so that EF can do its thing.
"Do I create a new model Users that acts as an interface so that I can implement custom user code?"
If you want flexibility, I would say this is the way to go. This way it would be easier if you wanted to change to some sort of different Authentication DB structure in the future.
For example, have an "AppUser" Entity where the corresponding table has a foreign key to the "UserID" column of the aspnet_Membership table. This way you can simply add properties to your "AppUser" Entity instead of trying to change the MS table structure (which can be a real pain). You can still interact with the built-in MS Membership classes and functions from your MVC project using something like the MvcMembership starter Kit DLL's.
https://github.com/TroyGoode/MembershipStarterKit
Hope this helps!
This has few preconditions:
ASP.NET tables must be in the same database as your own tables
Previous precondition means that you must either create your database and tables manually (without automatic code-first generation) or you must use some custom initializer which will add non mapped ASP.NET tables as part of database recreation
If you want your model class to have relation with ASP.NET table you must model ASP.NET table as another entity. I'm not sure if you can use ASP.NET classes for that because for example MembershipUser doesn't have parameterless public constructor which is required for EF. So you will most probably need to create duplicate classes and their mappings and use these classes when referencing ASP.NET entities.