© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
BakaPresident

❔ Entity Framework not required foreign key as required?

I'm currently using ASP.Net Identity as my user scaffold.
My user currently looks like
public class User : IdentityUser
{
    // Add additional data here
    public string DisplayName { get; set; }

    public ICollection<Bookshelf> Bookshelfs { get; } = new List<Bookshelf>();
}
public class User : IdentityUser
{
    // Add additional data here
    public string DisplayName { get; set; }

    public ICollection<Bookshelf> Bookshelfs { get; } = new List<Bookshelf>();
}

I've added UserGenre and Genre like this.
public class Genre
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public List<UserGenre> UserGenres { get; } = new();
}
public class Genre
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public List<UserGenre> UserGenres { get; } = new();
}

public class UserGenre
{
    public int Id { get; set; }
    
    public string UserId { get; set; }
    public User User { get; set; } = null!;
    
    public int GenreId { get; set; }
    public Genre Genre { get; set; } = null!;
}
public class UserGenre
{
    public int Id { get; set; }
    
    public string UserId { get; set; }
    public User User { get; set; } = null!;
    
    public int GenreId { get; set; }
    public Genre Genre { get; set; } = null!;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Entity Framework Foreign Key
C#CC# / help
2y ago
Foreign Key | Entity Framework
C#CC# / help
2y ago
Entity Framework Foreign Key restraint
C#CC# / help
16mo ago
Entity Framework Core 8 -Foreign Key Constraint failure.
C#CC# / help
2y ago