Create a Many to Many relationship between ApplicationUser and custom table - asp.net-mvc

I am a newbie to building website's in asp.net mvc5. I followed some tutorials and build my first website based on a existing database, so that's why i choose for database first approach.
Now I want to create with many to many relationship between the applicationuser object with my resource object.
from db point of view:
AspNetUsers <-> ApplicationUserResources <-> Resource
Which steps do I need to follow when using a database first approach to archive this relationship.
Wesley

You can add any navigation props in your ApplicationUser class as well as in any other EF Entity
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Resource> Resources{ get; set; }
}
public class Resource
{
public virtual ICollection<ApplicationUser > ApplicationUsers { get; set; }
}

Related

Integrate MVC Core Identity with other Database First Tables

I am using MVC Core with the default individual user authentication.
I added FullName field to AspNetUsers Table and it is working fine.
I need to create a relationship between my tables to the id field in the AspNetUsers table so I can get the fullname of the user.
I have no idea on how to create relationship in Entity framework, I tried to add the following in the ApplicationUser Class
public class ApplicationUser : IdentityUser
{
public string FullName { get; set; }
public mytable T { get; set; }
}
and
in mytable model, I added the following
public virtual ICollection<ApplicationUser> ApplicationUser { get; set; }
unfortunately, I am getting the following error:
The entity type 'IdentityUserLogin' requires a primary key to be defined
any idea?
The first issue solved by following link
https://forums.asp.net/post/6144294.aspx
the IdentityUserLogin' requires a primary key to be defined also solved by
https://stackoverflow.com/a/40824620/7046796

Identity Server 4 user model relations

I am quite new to .Net Core and Identity Server 4, I followed several tutorials and got an Identity Server with Asp .Net Core Identity running.
Then I have a separate API project with restricted routes that works well.
So let's say I want users to be able to post comments to this api.
In my Identity Server project I have a model called "ApplicationUser" extending "IdentityUser".
public class ApplicationUser : IdentityUser
{
}
In my API project I have a "Comment" model:
public class Comment
{
public long Id { get; set; };
public string Text { get; set; };
}
Now I want to know which User posted the comment, so I added a foreign key to the user and a pointer to ApplicationUser in my Comment model
public class Comment
{
public long Id { get; set; };
public string Text { get; set; };
public long UserId { get; set; }
public ApplicationUser User { get; set; }
}
But since "ApplicationUser" is in my IdentityServer project, I need to add a Project dependency in my Api project pointing to the IdentityServer project.
Then in my "ApplicationUser" model I want to have a one-to-many relation to the comments posted by that User, like this:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Comment> Comments { get; set; }
}
But here I am encountering a problem since my "IdentityServer" project doesn't have access to my "Comment" model in the API project and if I try to add dependency to that project then I get an error of "circular dependency".
So I guess I am doing this the wrong way. How can I access users from the IdentityServer project in my API project models and vice-versa in the best way?
Thanks!

Where is UserProfile or ApplicationUser model class inside MVC 5 application

I have a hard time finding UserProfile/ApplicationUser in the MVC 5 application. I would like to add this field: public virtual ICollection<Meeting> Meetings { get; set; } to UserProfile/ApplicationUser class.
I want do this to use Mr. Chris Pratt's advice: How to associate list of objects with user(Account) in ASP .NET MVC
EDIT: Will Entity Framework will understand the association between Meeting and USER if I add in class Meeting field public int ApplicationUserId { get; set; } in code first approach?
I got only this:

Custom membership provider or role my own implementation

I've started using the membership provider in a new asp.net mvc project. My membership model is quite dissimilar to the default model but I assumed I could customize it to my liking. It seams though that I'm creating a custom implementation for everything I'm doing.
My model has two types (inheriting from the same base) of User entities and a Customer entity.
public class Customer
{
/* properties go here */
// users linked with this customer
public virtual IList<TypeOneUser> TypeOneUsers { get; set; }
public virtual IList<TypeTwoUser> TypeTwoUsers { get; set; }
}
public abstract class User
{
/* user properties go here */
}
public class TypeOneUser: User
{
public virtual IList<Customer> Customers { get; set; }
}
public class TypeTwoUser: User
{
public virtual Customer Customer { get; set; }
}
I'm currently considering just implementing a Customer repository and User repository and scrapping the membership provider. So my question is, is there a straight forward way to implement my membership model with the membership provider? At the moment I have the User classes mostly implemented with a custom membership provider but I'm not sure how to integrate the Customer entity with the membership provider.
Also I'm using Fluent Nhibernate so I'm writing my own persistence code regardless of my approach.
You can take a look at my blog post on creating a custom membership provider. There is an example there, it uses Linq-to-Sql but I think it can give you a pretty good idea on how you can use your model to persist the data in the database.

Alternative User management in ASP.NET MVC

I am in the planning phase of a new ASP.NET MVC application and one of the requirements is storing some user information that is not part of the standard set found in the User class that comes with ASP.NET MVC. I suppose it comes down to two questions.
1) Can I edit the class that is being used already to store the information that I need?
2) If I roll my own how can I keep things like the Authentication piece that make things so nice when trying to lock down some views using the User.IsAuthenticated method?
Another alternative I have considered is using the User class provided as is, and instead putting the other information into a separate table with the guid userid as the foreign key.
Suggestions?
Profiles are one option as #Burt says, and offers a lot of flexibility.
I had a similar need to track Employee information, but I opted to roll my own Employee class and create a relationship to a standard User. I really like how this has worked out as I can keep any Employee specific business logic separate from the User class Membership system.
Since not every User was going to be bound with an employee, this made more sense for my case. It may not for yours, but it is an alternative.
So, I have something like:
public class Employee
{
public Employee(string name) : this()
{
Name = name;
}
public virtual string Name { get; set; }
public virtual string Title { get; set; }
public virtual decimal Salary { get; set; }
public virtual decimal Hourly { get; set; }
public virtual decimal PerDiem { get; set; }
public virtual string StreetAddress { get; set; }
public virtual Guid UserId { get; set; }
public virtual MembershipUser User {
get
{
// note that I don't have a test for null in here,
// but should in a real case.
return Membership.GetUser(UserId);
}
}
}
See ASP.Net MVC Membership Starter Kit. It provides the Asp.Net MVC controllers, models, and views needed to administer users & roles. It will cut distance in half for you.
Out of the box, the starter kit gives you the following features:
List of Users
List of Roles
User
Account Info
Change Email Address
Change a User's Roles
Look into profiles that are part of the membership functionality provided by MS. They are extendable and pretty flexible.

Resources