0

Is it possible to have ModelBuilders for Entities to be in different files? We are trying to separate automatic scaffolding from database, and have certain manual customization also.

This is what I am attempting, receiving error below,

File 1:

modelBuilder.Entity<PropertyMailingAddress>(entity =>
{
    entity.HasOne(d => d.LkAddressType)
        .WithMany(p => p.PropertyMailingAddress)
        .HasForeignKey(d => d.LkAddressTypeId)
        .HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");

File 2:

modelBuilder.Entity<PropertyMailingAddress>(entity =>
{

    entity.HasOne(d => d.LkSourceOfAddress)
        .WithMany(p => p.PropertyMailingAddress)
        .HasForeignKey(d => d.LkSourceOfAddressId)
        .HasConstraintName("FK_PropertyMailingAddress_LK_SourceOfAddressId");

Error CS1501 No overload for method 'Entity' takes 2 arguments

Is it possible to have partial class methodology for this?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

3 Answers3

2

If you want to have a file per Entity then, instead of using the Model Builder, use the IEntityTypeConfiguration. Create a class implementing IEntityTypeConfiguration<T> per entity. For example:

public class PropertyMailingAddressConfig : IEntityTypeConfiguration<PropertyMailingAddress>
{
  public void Configure(EntityTypeBuilder<PropertyMailingAddress> builder)
  {
     builder
       .HasOne(d => d.LkAddressType);
       .WithMany(p => p.PropertyMailingAddress);
       .HasForeignKey(d => d.LkAddressTypeId)
       .HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
  }
}  

The default conventions apply the same way, so a property called Id will automatically be mapped as Primary Key and so on.

You then need to tell Entity Framework to load the entity configurations from the files found in a given Assembly:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
      .ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); 
}
Francesc Castells
  • 2,692
  • 21
  • 25
1

You can do this by setting the dbcontext class to partial, creating the partial class in two files, and then defining a method in one of the files to pass the ModelBuilder parameters.

For details, please refer to the following.

File1:

  public partial class MyDbContext: DbContext
    {
        public DbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }
        public DbSet<PropertyMailingAddress> PropertyMailingAddress{ get; set; } 

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            OnModelCreatingPartial(modelBuilder);

            modelBuilder.Entity<PropertyMailingAddress>(entity =>
            {
              entity.HasOne(d => d.LkAddressType)
                    .WithMany(p => p.PropertyMailingAddress)
                    .HasForeignKey(d => d.LkAddressTypeId)
                    .HasConstraintName("FK_PropertyMailingAddress_LK_AddressTypeId");
            });  
        }
    }

File2:

     public partial class MyDbContext : DbContext
        {
            partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
            {
               modelBuilder.Entity<PropertyMailingAddress>(entity =>
               {    

                  entity.HasOne(d => d.LkSourceOfAddress)
                        .WithMany(p => p.PropertyMailingAddress)
                        .HasForeignKey(d => d.LkSourceOfAddressId)

 .HasConstraintName("FK_PropertyMailingAddress_LK_SourceOfAddressId");
               });
            }           
        }

You can refer to this.

LouraQ
  • 6,443
  • 2
  • 6
  • 16
0

I really don't know why you want to have separate configuration file for a single entity. However, based on what you have provided, you can do this:

public partial static class MyExtensions
{
 public static void ConfigureMyEntity(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.HasOne<Other>()
.WithMany()
....
}
}

If you want to define configurations for separate entities, there are cleaner ways to do that.

bolkay
  • 1,881
  • 9
  • 20