EF Core - Primary key values are not consistent in many to many relationship

c# entity-framework-core

Question

I have 2 tables Book and Author with many to many relationships. The implementation of many to many for Entity Framework Core (v. 2.2.1) was taken here: https://docs.microsoft.com/en-us/ef/core/modeling/relationships#many-to-many.

I can't figure out why when I add new Book with related Authors, using BookAuthor table as additional table to support many to many relationship, primary key for Author entity is not consistent I mean Id property goes like: 2, 4, 6, ... and not 1, 2, 3, ...

Here is my code:

public class Author
{
    public int Id { get; set; }
    public string FullName { get; set; }

    public ICollection<BookAuthor> BookAuthors { get; set; }
}

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }

    public ICollection<BookAuthor> BookAuthors { get; set; }
}

public class BookAuthor
{
    public int BookId { get; set; }
    public Book Book { get; set; }

    public int AuthorId { get; set; }
    public Author Author { get; set; }
}

public class AppDbContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
    public DbSet<BookAuthor> BookAuthors { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BookAuthor>() 
            .HasKey(x => new {x.BookId, x.AuthorId});

        modelBuilder.Entity<BookAuthor>() 
            .HasOne(pt => pt.Book)
            .WithMany(p => p.BookAuthors)
            .HasForeignKey(pt => pt.BookId);

        modelBuilder.Entity<BookAuthor>() 
            .HasOne(pt => pt.Author) 
            .WithMany(t => t.BookAuthors)
            .HasForeignKey(pt => pt.AuthorId);
    }
}

And here is how I add new Book with Authors:

    class Program
{
    static void Main(string[] args)
    {
        var options = new DbContextOptionsBuilder<AppDbContext>()
            .UseInMemoryDatabase("test")
            .Options;
        using (var context = new AppDbContext(options))
        {
           var book = new Book
           {
               Title = "Test Book",
               BookAuthors = new List<BookAuthor>
               {
                   new BookAuthor
                   {
                       Author = new Author
                       {
                           FullName = "Test name"
                       }
                   },
                   new BookAuthor
                   {
                       Author = new Author
                       {
                           FullName = "Author #2"
                       }
                   }
               }
           };
           context.Books.Add(book);
           context.SaveChanges();

        }
    }
}
1
1
2/10/2019 11:30:24 AM

Accepted Answer

In you example you use UseInMemoryDatabase this is not a proper DB it is just designed for testing, if you try your code with MS SQL Server or SQL azure, the generated Ids will be 1,2,3... as you expect it.

If you check the MS docs: https://docs.microsoft.com/en-us/ef/core/providers/in-memory/ "This database provider allows Entity Framework Core to be used with an in-memory database. This can be useful for testing, although the SQLite provider in in-memory mode may be a more appropriate test replacement for relational databases."

I tried you code with SQLite and the Ids are also ok there. So, this is not an EF Core issue, this related to the InMemoryDatabase.

2
2/10/2019 1:02:09 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow