Changing the table-name of AspNetUsers in ASP.NET Identity Framework - c#

I have 2 questions... I am using the Identity Framework and the Entity Framework for storing user data.
The default name of the User-Table is "AspNetUsers". Normally I use data annotations to change the table name to "Users" with:
[Table("Users")]
But this does not work here. Instead, I have to use the fluent API to achieve my goal:
modelBuilder.Entity<User>().ToTable("Users");
This works perfectly fine, but it's not the preferred way since I want to have my code consistent and renaming tables works fine with other data-classes with data annotations.
2) When I have changed the table-name via fluent-API I got another problem. The UserManager of the Identity-Framework can't find the table anymore. Is the table name hard-wired in the code? Can't believe that!?
Every time I am using the UserManager, for example, to create a new user, I get an exception that the table "AspNetUsers" can't be found. How can I fix that?

Related

Is it possible to have multiple Identity-Instances in the same DBContext?

We have an existing ASP.net Core 2.1 Application which has UserAccounts associated with the Identity-System.
Now we would like to add Admin-Users which shall not share the UserAccountsTable which is already used by the UserAccounts. (Yeah i know, we could simply add a bool column like isAdmin but we opted for seperated tables).
So my thinking was, that i need to create a new Identity-Instance which is using our AdminUser and AdminRole classes (Both deriving from IdentityUser and IdentityRole accordingly).
In the DBContext i can now change the Table-Names via the Fluent-API of AdminUser and AdminRole. But how would i now change the names of the needed "infrastructure" tables created by Identity automatically?
I've found this documentation by Microsoft. But they are only using "generic" types to rename the tables for e.G. the Link-Table between Users and Roles (IdentityUserRole). This type would probably "conflict" with my already configured UserAccounts-Identity, therefore "renaming" both tables, making troubles again, or not?
An obvious solution could be to create a new AdminUserContext, which would not share the UserAccounts-Information. But then we would lose the Links to the Entities we actually would want to administrate? And linking DBContexts doesn't feel "right" to me.
Anybody got some ideas to this?

Will migrations affect my database?

I am learning Entity Framework to query the database of my company. I have an ASP.NET MVC project and as of now, I have established a connection to the company's principal server database. That has given me access to all the tables and I created a separate class Library containing all the corresponding POCOs(generated automatically).
In the tutorial I was following they say to use "enable-migrations" to have the database updated with the model.
So does that mean that if I were to modify the models in my project, that would have a direct effect on the database? Since I am new to this project I do not want to do anything stupid, like altering the database. For now I just want to query the database and retrieve information, then use that information to show more or less information on a web page.
EDIT: Just as an example, I would like to show a difference between the model generated by EF and what my real table looks like. I have a table Web_Profils that contain and ID, a ProfileName and an Order (int). This DB has no primary keys or foreign keys. If there are relations, they are defined through new tables. But when EF generates all my classes, it adds ICollections, for example in Web_Profils, I have a.o virtual ICollection<Web_User_joint_Profils>Web_User_joint_Profils which is not present in the actual table, it just seems to be the relation that EF has deduced(it is the relation between Users and Profiles present in the table Web_User_joint_Profils). Now, will doing a migration affect my tables just because EF has added these collections in my model?
I've also read that it is possible to deactivate migrations using :
Database.SetInitializer(new ContextInitializerNone<YourDbContext>());
Any thoughts?
If you update your model, you need to add a migration to your project and update your database with that migration.
Unless you do those steps after updating your model, changes will not be reflected in the database.

Creating abnormal database queries with Entity Framework

I am working on a project where I may not alter the database in any way (unfortunately). I have started the project using Entity Framework and this has worked for the first few objects that I need. Now I have come across two scenarios that I am not sure how to accommodate.
Tables without a primary key defined.
Tables using the suffix of the table name as a field.
For the first, I get an error about reviewing my schema and uncommenting the proper area of the edmx file. The table has a field that acts as primary key but is not designated as not null and does not have a primary key created for it.
For the second, there are several tables with names like order1, order2, order3 etc where the table that needs to be accessed would be a parameter of my access methods.
My thought is that it would be simplest to just manually write the SQL and bind the data to models. If I go that route would I even use EF or do I just create a new database connection? what would be the 'proper' way to go about that?

How to make foreign key from Membership's users table to custom table in code first approach in MVC 4?

I am creating a MVC 4 application in Code First approach and using DefaultMembershipProvider and DefaultRoleProvider, but I am getting a problem in making foreign key of membership's users table to my custom table, which stores some additional information of users. Please provide me a way how can I do this.
Please suggest.
here's one way:
you create your tables using Code First entities. Then you go to your database schema, right click the schema, select "Relationships" and manually create the relationship to the membership table in sql server. You'll have to make sure that the membership table has been created in your database first.
Do you have the option of using SimpleMembershipProvider?
If you do, this post http://blog.spontaneouspublicity.com/including-asp-net-simple-membership-tables-as-part-of-your-entity-framework-model
has a simple explanation of how to integrate the membership tables with your EF.
But I'm not sure you need a complete integration with EF, so go through some SimpleMembershipProvider tutorials to see if it covers your requirements.
http://www.asp.net/web-pages/tutorials/security/16-adding-security-and-membership
If not I assume you'll need to model all the DefaultMembershipProvider tables in your EF model and add the relationship there.

MVC 3 Code first First with built-in membership authentication

I would like to use code first EF 4.1 to build my application. I'm using MVC 3. I would simultaneously like to use Membership authentication. The problem I see mainly is how do I do a foreign key to the membership users table that was generated by the tool, to the model i'm creating via code.
for example:
dbo.aspnet_Users which has a guid as it's PK
I want to create a user in MY user table (dbo.Users) with the same GUID as my PK.
Is there a way to form that association without it just being implied by the matching data?
I'm not quiete sure what you are trying to achieve. If you want to associate custom data to a user, you could use the profile.
Is there a special reason why you like to sort of "douplicate" some user information?

Categories