Entity structure first code: setting in another file

What is the best way to separate table mapping from objects using the Fluent API so that they are all in a separate class and not embedded in the OnModelCreating method?

What am I doing now:

public class FooContext : DbContext { // ... protected override OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Foo>().Property( ... ); // ... } } 

What I want:

 public class FooContext : DbContext { // ... protected override OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.LoadConfiguration(SomeConfigurationBootstrapperClass); } } 

How do you do this? I am using C #.

+6
source share
1 answer

You need to create a class that inherits from the EntityTypeConfiguration class, for example:

 public class FooConfiguration : EntityTypeConfiguration<Foo> { public FooConfiguration() { // Configuration goes here... } } 

You can then load the configuration class as part of the context as follows:

 public class FooContext : DbContext { protected override OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new FooConfiguration()); } } 

In this article, we will take a closer look at the use of configuration classes.

+16
source

Source: https://habr.com/ru/post/897632/


All Articles