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 #.
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.
Source: https://habr.com/ru/post/897632/More articles:3D graphics: normal display vs bump mapping? - directxjava: closing std thread subprocesses? - javaWhat are the image size limits for the Fb.ui feed? - imageProblem with JPA project in Eclipse - error in class annotated @Entity: Table "xxx" cannot be resolved - eclipseIs onCreate called when an Activity is created? - androidUnknown buffer size to read from DataInputStream in java - javalibcrypto deprecated on Mac OS X 10.7 (Lion) - osx-lionTesting the Nunit MVC Site - c #How to automatically close the quick fix window when exiting a file? - vimPhonegap iPad App Splash / Launch Screen Shifts on deviceReady - javascriptAll Articles