0

Am I able to use Entity Framework models (Classes) as a classes for asp.net Identity

so the relations that I have in my database will be loaded when I retrieve the user and if I update any columns or add tables I only have to deal with Entity Framework model.

I did do my custom classes for users

public class MyUser : IdentityUser<long, MyLogin, MyUserRole, MyClaim>{ ... }

and connected it with the table

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<MyUser>().ToTable("Users");
            .....
        }

but I have more tables that are connected with 'Users' table that isn't with Identity model for example:

I have a table called Person(Not really just an example)

and each person may have many users and each user may have many persons

So We have another table called 'PersonsUsers'

So the user have a list

I don't want to call the database twice to retrieve a single user data, and mapping the table from code will make my code static and depends on me updating the source code.

so Is it possible to use the classes that EF generated for the tables?

Do you have any other solution?

LeftyX
  • 35,328
  • 21
  • 132
  • 193
iYazee6
  • 826
  • 12
  • 22

1 Answers1

0

Yes, this is possible. Your MyUser class could have a property with the list of Person. then in your modelBuilder, you set up the mappings. Something like this:

 modelBuilder.Entity<MyUser>().HasMany(m => m.Person)
                              .WithMany(p => p.MyUser)
                              .Map(m => {
                                   m.ToTable("PersonUsers");
                                   m.MapLeftKey("MyUserID");
                                   m.MapRightKey("PersonID");

I believe this will accomplish what you're looking for.

Matt M
  • 3,699
  • 5
  • 48
  • 76
  • Thank you matty for your answer, but this will make my code static, I have to update it when ever I add or modify a relation, are there any way to use EF classes? also I have some field in user that I added and want to include how can I do that? – iYazee6 Dec 24 '14 at 06:45
  • I'm not sure I completely understand what you're looking to do. Perhaps you can give a more detailed example in your post? – Matt M Dec 24 '14 at 14:46