Search code examples
abp-framework

Abp One to many Relationship


I have Book and Author Entity. I tried to establish a relationship between Book and Author, but the AuthorId key did not appear. I wonder how it is done. enter image description here


Solution

  • First of all, I think you are looking at the tutorials part of the ABP framework document. If you haven't looked, I think you should look at this.

    As explained in the document, you can do this as follows:

    Open the BookStoreDbContextModelCreatingExtensions class under the EntityFrameworkCore folder of the Acme.BookStore.EntityFrameworkCore project and change the builder.Entity part as shown below

    builder.Entity<Book>(b =>
    {
      b.ToTable(BookStoreConsts.DbTablePrefix + "Books", BookStoreConsts.DbSchema);
      b.ConfigureByConvention(); //auto configure for the base class props
      b.Property(x => x.Name).IsRequired().HasMaxLength(128);
    
      // ADD THE MAPPING FOR THE RELATION
      b.HasOne<Author>().WithMany().HasForeignKey(x => x.AuthorId).IsRequired();
    });