Code-first relation to aspnet_users in C# - c#

How can I link a table created with the Code-First approach to the default Users table of ASP.NET? I am working with Entity Framework 4.3.1 and MVC3 in C#.
The project I am working on is meant for an organisation that organizes tournaments. When they create a tournament, a new user has to be created linked to that tournament so that when this user logs in, he can only work with that particular tournament.
Any idea how to make this relation with Code-First?
Thanks in advance,
Bart

I would deploy the tables to your database for reference, then copy them into models. Just make sure to get all the relationships correct for the FK, etc. You should then be able to have EF deploy the database and use the built-in membership tables.
If you ever upgrade to MVC4 with SimpleMembership, check out this example: http://blog.spontaneouspublicity.com/including-asp-net-simple-membership-tables-as-part-of-your-entity-framework-model

Related

C# AspNet Identity Web forms

I had an ASP Net web forms application with database using Database First Approach. I then merged Aspnet Identity Tables into my existing db. Then i used Entity Framework Reverse Enigneer extension to convert to Code First.
I got the Code First POCO classes & mappings for My existing db. However for Aspnet identity i got only one file i.e. IdentityModels.cs. NO Code First Model classes.... IS IT RIGHT ? Should i have got those POCO classes for AspNet identity tables as well ??
I also want to refer to User Id of the AspNetUsers table into my db tables...Since there are no POCO classes... How to refer to them ???
Further, There are now two contexts.. a) ApplicationDbContext derived from IdentityDbContext. b) MydbContext.... How to unify them if possible ???
Things seem scattered.
Not much documentation by Microsoft or otherwise...or the framework has become obsolete ????
Any help is appreciated.
It seems WebForms have been ditched starting VS 2017 & .NET Core.
Its time to move on to Razor Pages or MVC.

How to set up Identity in an ASP.Net core project using EF 6.0 in a separate class library

A while ago I asked this question about using EF with ASP.Net core, and this is working great. Now that I want to add ASP.Net Identity, I'm wondering if the best approach is to add identity logic to the separate project with regular / non-core EF, or into the main core project with EF core, or something else. I see a lot of pages that discuss EF core, but I would prefer to use the same context everywhere, and I would prefer to use regular EF as I am currently doing. I'm looking for what would be a clean / best practices approach here, I have been using regular EF since to my knowledge EF core 1.0 doesn't support stored procs, and I prefer to continue to use a database first approach anyway. Appreciate any help that gets me out of the quicksand.
NOTE: My 'User' table (called something else) has some existing profile data in it; I intend to use this as my Identity User table.
Have you considered having an explicit Identity service that can issue tokens based on your existing identity database?
IdentityServer4 is an OpenId Connect provider. It supports EF so that you can use your existing user data to perform authentication.
The push now is towards .NET Core and it would be a good step in that direction to have a separate project dedicated to handling identity that uses IdentityServer4 and EF Core.
ASP.Net Identity Framework is designed for Code-first, and will normally try to generate its tables for you. I'm not sure if it is able to work Database-first, but I think you could get it working as follows:
Create a separate, temporary, project/solution with Identity / Individual User Account.
Insert the data structure from this temporary database into your main database, merging AspNetUsers with your User table.
Delete the temporary project/solution and database.
Update the models in your EF6 project. This should bring in the new tables.
Add Identity Framework to your ASP.Net Core project, referencing the database context from your EF6 project.
I haven't tried this, but having worked with Identity Framework quite a bit I can't see a reason why it wouldn't work.
Further reading:
Database first Identity: Using Asp.Net Identity DataBase first approach
Custom table name for Users table: How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

EntityFramework Core code first two solutions

I have two solutions in c#:
One solution is identity server with asp.net core identity integration, which on first run creates identity user tables.
The second solution I have API, and project Data Layer, where I will implement custom tables.
How do I approach this architecture design, to make sure all migrations will know each other and there wont be data issues. In case I have to create additional table for AspNet_Users and link it with other table, I only have option to put it in Identity solution, but what if there are multiple links? How can I achieve proper architecture here?

ASP.NET MVC - Using Identity with existing, modified AspNetUsers table

At my company the DBA updates our SQL Server tables to add columns, etc and when pushing these changes to prod. In my scenario we have an existing AspNetUsers table and other Identity-related tables. This existing AspNetUsers table has additional columns added (FirstName, LastName, Joined date) and the id column was changed to an int.
I'm writing a new MVC Web Api (VS 2013) on top of this existing database and I started with the default Web Api template that uses Identity which I really like. I can't use Code-First since the tables already exist and already have these additional fields. I will not and cannot have my code update the DB through Nuget, and I'm not responsible for creating DB scripts either.
My question is: How can I have my code work with an existing AspNetUsers table that has additional fields that I'll need to capture when registering the user? I've added the new properties to the IdentityModel.cs inside of ApplicationUser : Identity User. I've also added these fields to my AccountController's Register method and the AccountBindingModel's RegisterBindingModel class. The error I'm getting when registering the user is "Mapping and metadata information could not be found for EntityType Phoenix.WebAPI.Models.ApplicationUser" and I don't know how to approach this since I'm not allowed to touch the DB and don't want to use Code-First.
Well, let's see:
At my company the DBA updates our SQL Server tables to add columns
As far as I understand about IT positions, an DBA should not do that. Database modelling is a job for an Analyst.
I can't use Code-First since the tables already exist and already have
these additional fields
That is not right. You can use Code-First in a existing database, it also works way better than EDMX. EDMX has been discontinued in EF7.
However, if you really don't want to use Code-First, take a look at this library https://github.com/kriasoft/AspNet.Identity it might be helpful.
You can try to use Fluent API to map your entities to database tables.
fluent api

Using own employee table to manage users in mvc using roles and identity

I am currently new to ASP .Net MVC and I'm working on a project. I already created my database which has a Employees table (which will consist of the users of the system) where I would want to manage all employees. I know mvc comes with a built in functionality that creates the roles and users table for you from code migration. How would I be able to implement that with my own database? Another problem, I tried creating a default and script the tables, added them to my database but the primary keys which is of type int doesn't match that of the id in the dbo.aspnetusers table that was created.
Can anyone assist me?
Regards
Your problem is bit unclear. but from what i have understood so far i can say that if your application is to be used internally, then you should consider using windows authentication and implement your own role provider.
Additionally you will need to provide more detail on what your are trying to achieve. this sounds to me like more of a design question rather than implementation question.

Categories

Resources