Asked by:
EF Core 5.0.X Model Customizations - database first

Question
-
User379720387 posted
I am modernizing a 4.X aspnet app to Blazor Server/Wasm.
In the original app I was using EF 6.4.X and a whole bunch of model customizations. Been looking for how I can stil use these but all I find is for code first approach.
namespace BtServer.Data { public class BtServerContext : IdentityDbContext<IdentityUser> { public BtServerContext(DbContextOptions<BtServerContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // Customize the ASP.NET Identity model and override the defaults if needed. // For example, you can rename the ASP.NET Identity table names and more. // Add your customizations after calling base.OnModelCreating(builder); } } }
How does this work in database first approach?
Saturday, May 22, 2021 4:35 PM
All replies
-
User1120430333 posted
Why would it be any difference in how the method is used between DB first and code first? Either the developer knew what he or she were doing when he or she did it manually with using code first, or the wizard did it when using DB first. But the overall usage of the method and how it is used would be the same between the two ways of how the model was created by using the wizard creation or manual creation using DB or code first.
Creating and configuring a model - EF Core | Microsoft Docs
The below code came from EF Core DB first, which did some configuring automatically. If Iwanted to do some more configuring beyond what DB first had initially done, I could do it.
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasAnnotation("ProductVersion", "2.2.2-servicing-10034"); modelBuilder.Entity<Article>(entity => { entity.Property(e => e.ArticleId).HasColumnName("ArticleID"); entity.Property(e => e.AuthorId).HasColumnName("AuthorID"); entity.Property(e => e.Body).HasMaxLength(400); entity.Property(e => e.Title).HasMaxLength(50); entity.HasOne(d => d.Author) .WithMany(p => p.Articles) .HasForeignKey(d => d.AuthorId) .HasConstraintName("FK_Article_Author"); }); modelBuilder.Entity<Author>(entity => { entity.Property(e => e.AuthorId).HasColumnName("AuthorID"); entity.Property(e => e.FirstName).HasMaxLength(50); entity.Property(e => e.LastName).HasMaxLength(50); }); modelBuilder.Entity<Payroll>(entity => { entity.Property(e => e.PayrollId).HasColumnName("PayrollID"); entity.Property(e => e.AuthorId).HasColumnName("AuthorID"); entity.HasOne(d => d.Author) .WithMany(p => p.Payrolls) .HasForeignKey(d => d.AuthorId) .OnDelete(DeleteBehavior.ClientSetNull) .HasConstraintName("FK_Payroll_Author"); }); } } }
Sunday, May 23, 2021 10:01 AM -
User379720387 posted
In my old EF6.4.X app I have a single file residing in App_Code called ModelCustomizer.cs.
It contains partial classes, which I now have read up on.
The Entity models already are decorated with "partial", so I can just copy the file into the same folder and it should work.
Would there be reasons why you would want model customizations to happen by means of the ModelBuilder?
Sunday, May 23, 2021 7:23 PM -
User1120430333 posted
Would there be reasons why you would want model customizations to happen by means of the ModelBuilder?
I can't say that a partial class is a means of model creation. EF wouldn't be concerned with persisting any thing beyond the original mappings of the properties of the virtual model class/object it is persisting. The usage of the partial class is to extend functionality at the class level not the ORM model class/object usage. I would think.
Differences of Entity Framework 6 and Entity Framework Core with Examples - SoftwareBlogs
Sunday, May 23, 2021 10:54 PM