Where is UserProfile or ApplicationUser model class inside MVC 5 application - asp.net-mvc

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:

Related

Create a Many to Many relationship between ApplicationUser and custom table

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; }
}

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!

ASP.NET MVC Entity Framework: Data Annotations

I'm working with Entity Framework with a database-first approach. I already defined the model inside my application. Now I'm working with controllers and views. I used scaffolding in order to create controllers. Now I want to create rows.
Let's say I want to create employees, and let's say the DBA and EF made this possible:
public partial class TBL_EMPLOYEE
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public TBL_EMPLOYEE()
{
this.TBL_EMPLOYEE = new HashSet<TBL_EMPLOYEE>();
}
public int EMPLOYEE_ID { get; set; }
public String CO_WORKER_NUMBER { get; set; }
public string NAME { get; set; }
public string LAST_NAME { get; set; }
public string SALARY { get; set; }
public string PHONE_NUMBER { get; set; }
public string EMAIL { get; set; }
public string { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
}
Now, I need a view to create an employee, let's call this view VIEW 1
In this view, the user only needs to specify name and last name values. Both are required.
Now, in this VIEW 1 case I could use the following data annotations attributes in the same class, that'd be:
[Required]
public string NAME { get; set; }
[Required]
public string LAST_NAME { get; set; }
Now, let's go to the next case. I need another View, let's call this VIEW 2
In this one, the user needs to specify all values for all attributes. All of them are required except for name and last name.
THE REAL QUESTION
How can I use the same model class for both views? The example above here might seem a bit silly and trivial validations but I've been in bigger projects where entities are bigger and the idea of having different ViewModel classes is just so much work.
I've stumbled upon this in my .NET developments, to the point I had to create a ViewModel class per view in order to be specific with what the user needs to input and their validation. Is this the only way?
To avoid duplicating models with minor variations, try this:
https://stackoverflow.com/a/18898112/6850962
Basically, create a base model with data annotations that apply in all situations (eg: DisplayName) and then extend the model for variations (eg: Required attribute).
If you are trying to use your Entity Framework entities approach, I wouldn't put validation attributes on those entities. I would either:
create separate distinct classes and then copy data from the EF entities to the models, and vice versa on update (either by writing the code explicitly, or using a tool like AutoMapper or many others). Then you can define the validation rules anyway you want. Unfortunately this approach does tightly couple validation to the model and thus model reuse may not be as possible.
Use a more fluid validation framework like FluentValidation (https://github.com/JeremySkinner/FluentValidation). The benefit to this is that you define an external class with the rules internal, which can be applied differently depending on the situation. The model may still need some indicator on the model itself to figure out which rules apply, but this is another functional approach to handling the scenario you describe.

Overly complicated many-to-many relationship with ASP.NET MVC

While researching whether or not ASP.NET MVC is suited for my next website, I've come across an annoying issue.
I have followed ASP.NET MVC since version 2, and it's gotten better. For instance, it's now fairly easy to get going with migrations in the entity framework with code first, which used to be a hassle.
This means that I now can get running with a database migrations and code first within half an hour (as I usually don't remember the steps involved, I have to follow a guide I wrote).
Now, fairly early on I always get a many-to-many relationship between entities (e.g. tags and posts) in my database, and what I've found is that getting this relationship exposed via MVC framework is surprisingly complicated! Example from asp.net Example from mikesdotnetting
It involves special methods to retrieve the relationship's data that is not an inherent part of the framework.
Is there really no better/easier way of treating the many-to-many relationship?
You should add a virtual key word to the Many port
public class Post
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
public virtual ICollection<Tag> Tags {get;set;}
}
public class Tag
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts {get;set;}
}

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