在单独的项目不能使用DbModelBuilder [英] Cannot use DbModelBuilder in separate project
问题描述
在我的解决方案,我有几个项目,一个Web应用程序的共同组成部分。
In my solution, I have a few projects, all together forming part of one web application.
我忙设立实体框架,并在我的上下文类,而不必写在我所有的流利的API code中的
I am busy setting up Entity Framework, and in my context class, instead of having to write all my fluent API code in the
protected override void OnModelCreating(DbModelBuilder modelBuilder)
方法,我希望能够在我的名为模式
类来创建一个方法 ConfigureModel
,然后配置所有主键/外键设置在那里,所以它看起来应该如下:
method, I want to be able to create a method in my Model
classes called ConfigureModel
, and then configure all the primary key/foreign key settings there, so it should look something as follow:
public class UserRolesModel : ModelBase
{
#region Configuration
public static void ConfigureModel(DbModelBuilder modelBuilder)
{
//necessary code here
}
#endregion
}
和,例如,调用方法如下:
And, for example, call the method as follow:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
UserRolesModel.ConfigureModel(modelBuilder);
}
我现在面临的问题是在我的 ConfigureModel
方法:我似乎无法解析 DbModelBuilder
引用。即使我添加了 System.Data.Entity的
DLL我的引用,它没有工作。我需要实体框架安装到这个单独的项目,以及为这个工作。
The problem I am facing is in my ConfigureModel
method: I cannot seem to resolve the DbModelBuilder
reference. Even if I added the System.Data.Entity
dll to my references, it did not work. I needed to install Entity Framework to this separate project as well for this to work.
当然,我没有EF添加到每个项目,我想使用 DbModelBuilder
吗?
Surely I do not have to add EF to every project I would like to use the DbModelBuilder
in?
什么是实现这一目标的另一种方式?
What is another way of achieving this?
感谢
推荐答案
下面是我的2美分既然你问。不要把流利code在你的模型 - 这是关注和杂波它们(非POCO)不良分离。一个很好的替代巨人OnModelCreating是这样的:
Here's my 2 cents since you asked. Don't put fluent code in your models - that's bad separation of concerns and clutters them up (non-POCO). A good alternative to a giant OnModelCreating is this:
1)请在您的项目有上下文称为EntityConfigurations的文件夹。在一个大的项目,你可以把它连起来像进一步EntityConfigurations \\ SYSTEM,EntityConfigurations \\员工等。<一个href=\"http://odeto$c$c.com/blogs/scott/archive/2011/11/28/composing-entity-framework-fluent-configurations.aspx\"相对=nofollow>从斯科特 - 阿伦
1) Make a folder in your project that has your context called "EntityConfigurations". In a large project you could divide it up even further like "EntityConfigurations\System", "EntityConfigurations\Employee", etc. From Scott Allen
2)添加类为每个项目:
2) Add classes for each item:
public class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
HasKey(p => p.ProductId);
Property(p => p.ProductName)
.IsRequired()
.HasMaxLength(200);
...
}
}
3)使用AddFromAssembly你的映射类将被发现并自动运行。 朱莉·勒曼。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Configuration.LazyLoadingEnabled = false;
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
}
这为我们的团队运作良好。你几乎看以往的流畅code因此保持它我们看一下每天的模式是个好东西。
This has worked well for our team. You hardly ever look at the fluent code so keeping it out of the model we look at daily is a good thing.
这篇关于在单独的项目不能使用DbModelBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!