© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
6 replies
prodijay

Unable to set User FK in another model (User extends IdentityUser)

I am attempting to establish relationships between my models. For context I'm using EntityFrameworkCore and Identity.
I have a User model (extends IdentityUser) and a Study model.
Studies should have one User, and Users can have multiple Studies.
The first time I tried to establish the relationships I wrote them in the models, like so:
  // Study.cs
  public class Study
  {
    public int Id { get; set; }
    //...
    public required string UserId { get; set; }
    public IdentityUser? User { get; set; }
  }
  // Study.cs
  public class Study
  {
    public int Id { get; set; }
    //...
    public required string UserId { get; set; }
    public IdentityUser? User { get; set; }
  }


  // User.cs
    public class User : IdentityUser
  {
    public List<Study> Studies { get; set; } = new List<Study>();
  }
  // User.cs
    public class User : IdentityUser
  {
    public List<Study> Studies { get; set; } = new List<Study>();
  }


That resulted in the warning
The foreign key property 'Study.UserId1' was created in shadow state because a conflicting property with the simple name 'UserId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type.
The foreign key property 'Study.UserId1' was created in shadow state because a conflicting property with the simple name 'UserId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type.


I am now trying to use OnModelCreating to create the relationships in
DBContext
DBContext
instead, like so:
  public class ApplicationDbContext : IdentityDbContext<IdentityUser>
  {
    public DbSet<User> Users { get; set; }
    public DbSet<Study> Studies { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
      base.OnModelCreating(builder);

      builder.Entity<Study>()
      .HasOne(s => s.User)
      .WithMany(u => u.Study)
      .HasForeignKey(s => s.UserId)
    }
  }
  public class ApplicationDbContext : IdentityDbContext<IdentityUser>
  {
    public DbSet<User> Users { get; set; }
    public DbSet<Study> Studies { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
      base.OnModelCreating(builder);

      builder.Entity<Study>()
      .HasOne(s => s.User)
      .WithMany(u => u.Study)
      .HasForeignKey(s => s.UserId)
    }
  }

However this says that
IdentityUser does not contain a definition for Study and no accessible extension method 'Study' accepting a first argument of type 'IdentityUser' could be found
IdentityUser does not contain a definition for Study and no accessible extension method 'Study' accepting a first argument of type 'IdentityUser' could be found


I have henceforth changed
public IdentityUser? User { get; set; }
public IdentityUser? User { get; set; }
to
public User? User { get; set; }
public User? User { get; set; }
with the same result.
However User should contain a list of
Study
Study
objects...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Unable to use IdentityUser
C#CC# / help
3y ago
✅ [DDD] Reference IdentityUser in Domain model class
C#CC# / help
3y ago
MVC EF 5 - How to reference Model FK Properties?
C#CC# / help
4y ago