实体框架代码优先 - 在另一个文件中配置
使用 Fluent API 将表到实体的映射分开的最佳方法是什么,以便它全部位于单独的类中,而不是内联在 OnModelCreating 方法中?
我目前正在做什么:
public class FooContext : DbContext {
// ...
protected override OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Foo>().Property( ... );
// ...
}
}
我想要什么:
public class FooContext : DbContext {
// ...
protected override OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.LoadConfiguration(SomeConfigurationBootstrapperClass);
}
}
你如何做到这一点?我正在使用 C#。
What is the best way to separate the mapping of tables to entities using the Fluent API so that it is all in a separate class and not inline in the OnModelCreating method?
What I am doing currently:
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#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要创建一个继承自 EntityTypeConfiguration 的类class,如下所示:
然后您可以将配置类加载为上下文的一部分,如下所示:
本文详细介绍了如何使用配置类。
You will want to create a class that inherits from the EntityTypeConfiguration class, like so:
Then you can load the configuration class as part of the context like so:
This article goes into greater detail on using configuration classes.