Search code examples
asp.netentity-frameworkasp.net-identity

How do you add a new user and claim in a single transaction using ASP.NET Core 2.1 Identity?


I am trying to add a new user and some other associated entities including a claim as one transaction. My classes are basically defined as below. Note that I am using int primary keys on my User class and that it is an identity column (value is provided by the database on insertion).

public class User : IdentityUser<int>
{
    // custom props here
}

public class UserClaim : IdentityUserClaim<int>
{
    // i actually haven't added anything to this class, it's mainly here so 
    // i don't have to keep specifying the user's primary key type
}

public class OtherEntity 
{
    public int UserId { get; set; }

    [ForeignKey(nameof(UserId))]
    public User User { get; set; }

    // other stuff
}

I then want to add the user etc. to the database something like this:

User user = new User(){ /* set props */ };

OtherEntity other = new OtherEntity()
{
    User = user
};

UserClaim claim = new UserClaim()
{
    /* this is where the problem is */
    UserId = user.Id,
    ClaimType = "my-claim-type",
    ClaimValue = "my-claim-value"
};

context.AddRange(user, other, claim);
context.SaveChanges();

I can easily link the User to the OtherEntity because I have set up the navigation property so I can just add the User to it and entity framework takes care of the filling in the UserId column. I cannot do this with UserClaim because it doesn't have the navigation property. I could call context.SaveChanges() after adding the User and entity framework would get the User.Id created by the database for me which I could use to set UserId on the UserClaim, but that would mean two transactions.

I have tried adding the navigation property to my definition of UserClaim as follows:

public class UserClaim : IdentityUserClaim<int>
{
    [ForeignKey(nameof(UserId))]
    public User User { get; set; }
}

But I get following runtime error:

InvalidOperationException: The relationship from 'UserClaim.User' to 'User' with foreign key properties {'UserId' : int} cannot target the primary key {'Id' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.

Is there a way of creating both the user, and the claim in the same transaction?


Solution

  • My question should have been: "How do I add navigation properties between ASP.NET Identity classes?"

    If I had looked for the answer to that I would have found the microsoft docs explaining how to do it!

    The docs linked above tell you how to add the User.UserClaims navigation property:

    public class User : IdentityUser<int>
    {
        public virtual ICollection<UserClaim> Claims { get; set; }
    }
    
    public class DataContext : IdentityDbContext</* Identity classes */>
    {
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<User>(e =>
            {
                e.HasMany(u => u.Claims)
                 .WithOne()
                 .HasForeignKey(c => c.UserId) 
                 .IsRequired();
            });
        }
    }
    

    But it doesn't show how to make the reverse UserClaim.User navigation property. I worked out that this can be done like this:

    public class UserClaim : IdentityUserClaim<int>
    {
        public virtual User User { get; set; }
    }
    
    public class DataContext : IdentityDbContext</* Identity classes */>
    {
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<User>(e =>
            {
                e.HasMany(u => u.Claims)
                 .WithOne(c => c.User) // <- this line is different
                 .HasForeignKey(c => c.UserId) 
                 .IsRequired();
            });
        }
    }
    

    You can then create a new user and claim at the same time as per my question:

    User user = new User(){ /* set props */ };
    
    UserClaim claim = new UserClaim()
    {
        User = user, // <- this is the bit you can't do without the nav. property
        ClaimType = "my-claim-type",
        ClaimValue = "my-claim-value"
    };
    
    context.AddRange(user, claim);
    context.SaveChanges();
    

    I guess it's common sense, though I didn't realise until I inspected the actual SQL hitting the database, but this will still require two trips to the database even though we are only calling .SaveChanges() once! Entity Framework will first save the User and get the ID for the inserted row which it will then use when inserting the UserClaim.