How to insert many to many without know join table model?

Viewed 18

How to access join table that generated by migrations. i know the table name that generated UserAccount. in old ways it simply create UserAccount model and apply to DbContext. but current entityframework can auto create join table in behind. but i dont know how to access it.

public class User
{
    public long Id { get; set; }
    public string Username { get; set; } = null!;
    
    public ICollection<Account> Accounts { get; set; }
}

public class Account 
{
    public long Id { get; set; }
    public string AccountId { get; set; }
    
    public ICollection<User> Users { get; set; }
}

public class ApplicationContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Account> Accounts { get; set; }
}
                    
public class Program
{
    public static void Main()
    {
        var context = new ApplicationContext();
        var user = new User { Username = "admin" };
        var account = new Account { AccountId = "190273723627" };
            
        context.Users.Add(user);
        context.Accounts.Add(account);
        context.SaveChanges();
    }
}
0 Answers
Related