Performance on queries with string filter vs foreign key

3

I'm working on a Asp.Net MVC 5 project that uses code first and all entities have a property named UserId of type string . When I make queries filtering by a certain user ( Asp.Net Identity ) I do so:

string currentUserId = User.Identity.GetUserId();
var products = await db.Products.Where(_ => _.UserId == currentUserId).ToListAsync();

Am I losing performance for not having a foreign key relationship and just saving UserId on a string property?

    
asked by anonymous 20.08.2015 / 19:18

1 answer

1

I'm losing performance by not having a foreign key relationship and just saving UserId on a string property?

It is. In fact, you can normally associate your ApplicationUser with any other entity, taking just a few precautions.

For the relationship between entities to work, care must be taken to envelop the context with RoleManager and UserManager as follows:

UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

In any case, if you feel that you are experiencing a lot of performance loss, I suggest reimplementing your ApplicationUser using as a key Guid or int :

public class MeuUserComGuid : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
{
    public User()
    {
        Id = Guid.NewGuid();
    }

    public User(string name) : this() { UserName = name; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}
    
20.08.2015 / 20:05