Performance in string filter queries vs foreign key

Asked

Viewed 238 times

3

I’m working on a project Asp.Net MVC 5 using code first and all entities have a property called UserId of the kind string. When I will 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();

I’m losing performance by not having a relationship through foreign key and just keeping the UserId on a property string?

  • 1

    A little vent: a thing that discouraged me in the Asp.Net Identity was the UserId be a Guid saved as string, even if there is no relationship between Products and users, you can create an Info for the Userid column in Products

  • Hi, @Tobymosque. If I add [Index] on top of my Userid property the query will be enhanced or need to do more?

1 answer

1

I’m losing performance by not having a relationship through foreign key and just keeping the UserId on a property string?

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

In order for the relationship between entities to work, care must be taken to envelop the context with the RoleManager and the 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 you are experiencing a lot of performance loss, I suggest you reimplement your ApplicationUser using as 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; }
}
  • The problem is I’m with the Entidades in a separate project and has two others who make use of it: the first is a Asp.Net MVC 5, which uses standard authentication of Asp.Net Identity and another Web Api 2, that uses via authentication Token of Asp.Net Identity. I mean, I can’t relate to my Entidades with Applicationuser, as it is within separate projects. I tried to put them together in a single project to create these relationships, but I couldn’t use Identity in both ways. Do you know any alternative?

  • In fact you can, but need to separate only the layer Model of the others, in a class library. This involves installing ASP.NET Identity on class library also.

  • I installed the Identity in the layer Model, add the property public IdentityUser User { get; set; } in the entity Product to create the relationship and to give update-database of that error: The ALTER TABLE statement conflicted with the FOREIGN KEY Constraint "Fk_dbo.Aspnetusers_dbo.Identityusers_id". The Conflict occurred in database "Chefappdb", table "dbo.Identityusers", column 'Id'. What could possibly go wrong? Note: The class ApplicationUser is only created in the archive IdentityModels when creating a project Asp.Net Mvc or Web Api, so I used the IdentityUser.

  • This happens when the Migration tries to create FK. Since FK is non-null, the error occurs. Make a backup of the database, delete the data from the tables involved and try again.

  • I don’t understand where I add code UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); and what would be the context?

  • The DbContext. The declaration shall be Controller where you need to select and manipulate users. You need to encapsulate this way, otherwise the EF understands that it has two open contexts.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.