catu
catu

Reputation: 898

Using ASP.Net Identity with model classes

I'm new to ASP.Net Identity, and I'm looking for a good tutorial for using Identity in conjunction with other classes in my model.

As an example I have a basic Ratings class that looks like this (from a project not using Identity)

public class Rating
    {
        public int Id { get; set; }
        public Product Product { get; set; }
        public User User { get; set; }
        public int Stars { get; set; }
        public string Comment { get; set; }
        public bool Active { get; set; }

    }

And a User Class that looks a bit like this

public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public ICollection<Rating> Ratings { get; set; }
    }

Looking for a way to achieve the same with Identity. My test project is set up with MVC5, and code first

Upvotes: 1

Views: 740

Answers (3)

Christian Sauer
Christian Sauer

Reputation: 10889

As a related hint: Watch out when you implement a Unit of Work pattern in your project. ASP.NET identities datacontext needs to be the same as the Uow datacontext, otherwise the whole think will crash.

A good starting point may be this: ASP.NET Identity with Repository and Unit of Work

Upvotes: 0

Ross
Ross

Reputation: 323

I agree with Rui.

Here is a site that will teach you How to Extend Identity Accounts and also Implement Based Authentication. When I was starting with Identity, that site taught me a lot.

Upvotes: 1

Rui
Rui

Reputation: 4886

The recommended solution is to add the properties you need to the ApplicationUser class

You can also use your own "User table", in your case that would be the User class. You'd have to inherit from IdentityUser.

This article has examples of how to do both.

Upvotes: 3

Related Questions