MS实体框架4.1中的Model First和Code First之间是否存在性能差异? [英] Is there a performance difference between Model First and Code First in MS Entity Framework 4.1?

查看:132
本文介绍了MS实体框架4.1中的Model First和Code First之间是否存在性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始一个新的开发,我计划在实体框架4.1中使用Code First。



我以前使用过Model First,发现了一些关于加载上下文的问题,首先调用SaveChanges()以及Association Fix-up所在的地方。

$有没有人比较这两种技术的表现呢?或者说在一天结束时它们是微不足道的?



谢谢。 p>

解决方案

我相信性能没有任何区别。 Code-First,Model-First,Database-First是设计时间的建模策略。对于Model-First和Database-First实体类,将使用T4模板创建一个 DbContext 。在运行时中,EF 4.1只适用于这些类,它们来自于手写(Code-First)或从T4模板(Model-First,Database-First)自动生成并不重要,请注意,Model-First给您的好处在我看来是相当有限的:您只需在设计表面上创建模型类即可。在VS2010。但是另一方面还有更多的缺点:在模型中创建代码时, T4模板不太精细。例如:它不会在属性上放置 MaxLength 属性,它在关系上双方创建导航属性(您通常不需要双方)并且 DbContext 中重写的 OnModelCreating 方法只包含单行 throw new UnintentionalCodeFirstException(); 这不是特别令人印象深刻。您可以修改EDMX文件的模板和CSDL部分,以便在生成模型类和DbContext时实现更多的粒度(谢谢Ladislav在下面关于此选项的评论)。



换句话说,您必须调整生成的代码(添加属性,删除未完成的导航属性,添加Fluent映射代码等),以获得要使用的finetuned模型类。一旦你这样做,变得很难在模型设计师做任何改变,因为DbContext生成器将覆盖代码中的所有手工修改。



在我的意见Model-First与EF 4.1只有在设计人员表面设计的模型(例如从旧的EF 4.0项目中)设计时才有用,并且您希望将项目迁移到EF 4.1。在这种情况下,DbContext生成器可能对为您创建初始代码。从那时起,我将继续在代码中工作,这意味着:使用Code-First。如果您从一个新项目开始,我将从头开始就喜欢Code-First。即使您真的想要或需要设计师表面中的模型的可视化表示,也可以在Code-First中,您可以从DbContext创建一个EDMX文件,并在VS2010中打开它,以在设计器中显示您的模型类及其关系: / p>

  using(var context = new MyDbContext())
{
using(var writer = new XmlTextWriter @c:\MyModel.edmx,Encoding.Default))
{
EdmxWriter.WriteEdmx(context,writer);
}
}

修改



实际上有一点,EF 4.1识别模型是否来自Model-First(即EDMX模型文件和设计器表面),或者是纯粹的Code-First模型 - 这是连接字符串。如果您从Model-First创建模型,您将获得一个连接字符串,其中包含对模型元数据文件的引用,如下所示:

  < add name =MyConnectionString
connectionString =metadata = res://*/Model.csdl | res://*/Model.ssdl
| res://*/Model.msl ; provider = System.Data.SqlClient;
provider connection string =& quot; data source = .\sqlexpress;
initial catalog = MyDb; integrated security = True;
multipleactiveresultsets = True ;应用=&的EntityFramework放大器; QUOT;
providerName =System.Data.EntityClient/>

而对于代码优先使用简单的一个没有元数据引用的正常连接字符串:

 < add name =MyConnectionString
connectionString =Server = .\SQLEXPRESS; Database = MyDb; Trusted_Connection = Yes ;
providerName =System.Data.SqlClient/>

现在,您可以使用第二个简单的连接字符串,也可以通过Model-第一。但是上面的代码片段(从DbContext创建一个EDMX)会引发一个异常,第一个连接字符串告诉 WriteEdmx 只能用于Code-First而不是Model-First数据库第一。显然,DbContext处理或存储来自连接字符串的元数据信息。



如何解释这个?这是否意味着在模型内置内存时,实际使用连接字符串中指定的EDMX文件中的数据?在这种情况下,理论上可以在Code-First和Model-First(至少在模型构建时)之间存在性能差异。但我不认为元数据是实际处理的。但是提到的异常有点奇怪。当我的模型来自Model-First时,为什么EF 4.1阻止我创建一个EDMX模型文件?也许只是为了避免可能的混乱和混乱的两个EDMX文件?我不知道。


I'm starting a new development and I plan to use Code First in Entity Framework 4.1.

I have previously used Model First and found some performance issues around loading context, first calls to SaveChanges() and where Association Fix-up kicks in.

Has anyone compared performance of these two techniques - or, at the end of the day are they insignificant?

Thanks.

解决方案

I believe that there is no difference at all in performance. Code-First, Model-First, Database-First are modelling strategies at design time. For both Model-First and Database-First entity classes and a DbContext will be created with a T4 template. At runtime EF 4.1 just works with those classes and it doesn't matter where they come from - hand-written (Code-First) or autogenerated from the T4 template (Model-First, Database-First).

Also keep in mind that the benefit that Model-First gives you is rather limited in my opinion: You just have the possibility to create your model classes on a design surface in VS2010. But there are more drawbacks on the other side: The default T4 template isn't very fine granular in creating the code from the model. For instance: It doesn't put MaxLength attributes on the properties, it creates always navigation properties on both sides on a relationship (you often don't want and need both sides) and the overridden OnModelCreating method in DbContext just contains the single line throw new UnintentionalCodeFirstException(); which isn't particularly impressive. You can modify the template and the CSDL part of the EDMX file though to achieve more granularity when the model classes and the DbContext are generated (thanks to Ladislav for his comment below about this option).

In other words it is very likely that you have to tweak the generated code (adding attributes, removing unwished navigation properties, adding Fluent mapping code and so on) in order to get the finetuned model classes you want to work with. As soon as you have done this it becomes difficult to do any changes in the model designer because the DbContext generator will overwrite all your hand-made changes in the code.

In my opinion Model-First with EF 4.1 is only useful if you already have a model designed in the designer surface for instance from an older EF 4.0 project and you want to migrate your project to EF 4.1 In this case the DbContext generator might be useful to create initial code for you. From that point I would proceed with working in the code alone which means: Working with Code-First. If you start with a new project I would prefer Code-First from the beginning. Even if you really want or need this visual representation of the model in the designer surface also in Code-First you can simply create an EDMX file from your DbContext and open it in VS2010 to show your model classes and their relationships in the designer:

using (var context = new MyDbContext())
{
    using (var writer = new XmlTextWriter(@"c:\MyModel.edmx", Encoding.Default))
    {
        EdmxWriter.WriteEdmx(context, writer);
    }
}

Edit

There is actually one point where EF 4.1 recogizes the difference whether a model comes from Model-First (i.e. an EDMX model file and designer surface) or if it is a pure Code-First model - and that is the connection string. If you create a model from Model-First you get a connection string which contains references to the model metadata files, like so:

<add name="MyConnectionString" 
     connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl
         |res://*/Model.msl;provider=System.Data.SqlClient;
         provider connection string=&quot;data source=.\sqlexpress;
         initial catalog=MyDb;integrated security=True;
         multipleactiveresultsets=True;App=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />

Whereas for Code-First simply a "normal" connection string without metadata references is used:

<add name="MyConnectionString" 
     connectionString="Server=.\SQLEXPRESS;Database=MyDb;Trusted_Connection=Yes;"
     providerName="System.Data.SqlClient" />

Now you can use the second simple connection string without problems also for a model which is created via Model-First. But the code snippet above (creating an EDMX from DbContext) throws an exception with the first connection string telling that WriteEdmx can only be used with Code-First but not Model-First or Database-First. So obviously the DbContext processes or stores somehow the metadata information from the connection string.

How to interprete this? Does it mean that it actually uses the data in the EDMX file specified in the connection string when the model is built in memory? In this case there could theoretically be a performance difference between Code-First and Model-First (at least at model-build-time). But I don't think that the metadata are actually processed. But the mentioned exception is somewhat weird. Why does EF 4.1 prevent me to create an EDMX model file when my model comes from Model-First? Perhaps just to avoid possible confusion and mess with two EDMX files? I don't know.

这篇关于MS实体框架4.1中的Model First和Code First之间是否存在性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆