How to correctly implement a link to another person for an Identity user?

I am using Identity, which has its own context.

public class ApplicationUser : IdentityUser { // some custom fields } public class IdentityContext : IdentityDbContext<ApplicationUser> { //... } 

I also have other objects like this

 public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} } 

which are used in my other context

 public class MyContext :DbContext { public DbSet<Comment> Comments { get; set; } //... other DbSets } 

Question. I want the object of my comment to have an author property, so I will have something like

 public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} public virtual ApplicationUser Author {get;set;} } 

but ApplicationUser is in a different context, albeit in the same database. I bet it's impossible.

How to implement this correctly? Should I move DbSets from MyContext to IdentityContext, so I can freely use code like this

 public virtual ApplicationUser Author {get;set;} 

or should I leave it in a different context, but add something like

 public string AuthorId {get;set} 

and do some workarounds to get author information from different contexts every time I need it? Or something else?

thanks


Edit

Ok, I got something like this:

 public class ApplicationUser : IdentityUser { public virtual UserProfile UserProfile { get; set; } } public class UserProfile { [Key, ForeignKey("ApplicationUser")] public string Id { get; set; } //... custom fields public virtual ApplicationUser ApplicationUser { get; set; } } public class IdentityContext : IdentityDbContext<ApplicationUser> { //... public DbSet<UserProfile> UserProfiles { get; set; } } 

But how should I use the link to the author’s comment? Like this? That way, it won't be linked through an EF relationship, and will I just populate UserProfileId somewhere in the code myself?

 public class Comment{ public int Id {get;set;} public string UserProfileId{get;set;} } 

Is this the right thing to do?

+1
c # asp.net-mvc entity-framework asp.net-identity dbcontext
source share
2 answers

Ask yourself, what information does ApplicationUser have that you can use in your business model? And if so, is this the right place to store it? Or do you just want to associate a user?

ApplicationUser is in a different context, albeit in the same database.

But now suppose this is not so. Suppose you want to use something like IdentityServer in the future.

I think the best approach would be to keep your business information separate from your personal information. I would not want to disclose registration information for a business with the possibility that it may be read or modified.

I saw code in which ApplicationUser (as part of the business context) was sent to the ViewModel to the client, including the HashPassword. Definitely what you want to prevent.

What you can do is add a user table to MyContext to store the data you want to use. There is no information in your ApplicationUser business model.

I assume that all you want is to associate the information with the user. And you want to profit from the binding of an Entity Framework object.

So, create a User table and add the ApplicationUser property to store the UserId table of your account. Or you can link another way: add ApplicationUserId to the user table. You can also use the same identifier for both: install ApplicationUser.Id yourself (it is not necessary to be a guide) or use the generated guid for User.Id.

If there is any additional information in the context of the identification that you want to use, for example. EmailAddress, you may consider adding claims.

- update -

The idea is to add a custom table to your context, and not to the identity context. To make it more understandable, I will call the table Person (not the user). Note that Person does not inherit IdentyUser / ApplicationUser.

 public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } //etc.. public string ApplicationUserId { get; set; } } public class MyContext :DbContext { public DbSet<Comment> Comments { get; set; } public DbSet<Person> Persons { get; set; } //... other DbSets } public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} public virtual Person Author {get;set;} } 

Now, when I request all comments for the current user, I can find Person.Id (based on User.Identity.GetUserId ()).

Do not forget to add a person when creating a login.

Hope this helps. If not, let me know.

+3
source share

Example:

 public class ApplicationUser : IdentityUser { // some custom fields } public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} public string AuthorId {get;set} [ForeignKey("AuthorId")] public virtual ApplicationUser Author {get;set;} } public class MyContext :IdentityDbContext<ApplicationUser> { public MyContext(): base("DefaultConnection", false){ } public DbSet<Comment> Comments { get; set; } //... other DbSets } 

I always pass this template. Hope this helps you.

0
source share

All Articles