Is there a performance difference between First Model and First First in MS Entity Framework 4.1?

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

Earlier, I used the First model and found some performance problems around the loading context, first calls SaveChanges () and where the fix-up merge occurs.

Has anyone compared the performance of these two methods - or, in the end, are they immaterial?

Thanks.

+6
source share
3 answers

I believe that there is no difference in performance. First, code, model-first, database-first - modeling strategies during development . A T4 template will be created for the Model-First and Database-First and DbContext object classes. At run time, EF 4.1 only works with these classes, no matter where they come from - handwritten (Code-First) or auto-generated from the T4 template (Model-First, Database-First first).

Also keep in mind that the advantage that model one gives you is, in my opinion, quite limited: you just have the ability to create model classes on the design surface in VS2010. But there are other drawbacks on the other hand: the default T4 template is not very thin to create code from the model. For example: it does not set MaxLength attributes in the properties, it always creates navigation properties on both sides of the relation (you often do not need and need both sides) and the overridden OnModelCreating method in DbContext simply contains a single line throw new UnintentionalCodeFirstException(); which is not particularly impressive. You can change the template and part of the CSDL in the EDMX file, but to achieve more detail when creating model classes and DbContext (thanks Ladislav for his comment below about this option).

In other words, it is very likely that you need to customize the generated code (add attributes, remove unwanted navigation properties, add Fluent display code, etc.) to get the classes of the final models that you want to work with. Once you do this, it becomes difficult to make any changes to the model designer, because the DbContext generator will overwrite all your manual changes to the code.

In my opinion, Model-First with EF 4.1 is useful if you already have a model developed on the surface of the designer, for example, from the old EF 4.0 project, and you want to transfer your project to EF 4.1. In this case, the DbContext generator may be useful for creating the source code for you. From now on, I would continue to work only in code, which means: Working with Code-First. If you start with a new project, I would prefer Code-First from the start. Even if you really want or need this visual representation of the model on the surface of the designer, 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

In fact, there is one point at which EF 4.1 reviews the difference: whether the model comes from the First model (i.e. the EDMX model file and the designer surface) or if it is a clean Code-First model and this is a connection string. If you create a model from Model-First, you will get a connection string containing links to model metadata files, for example:

 <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, only the "normal" connection string is used without metadata references:

 <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 any problems also for the model created using the First model. But the code snippet above (creating EDMX from DbContext) throws an exception with the first connection string, which says that WriteEdmx can only be used with Code-First, but not with Model-First or Database-First. Thus, it is obvious that DbContext processes or stores somehow metadata information from the connection string.

How to interpret this? Does this 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 during model build). But I do not think that metadata is being processed. But the specified exception is somewhat strange. Why doesn't EF 4.1 allow me to create an EDMX model file when my model comes with the First model? Perhaps just to avoid confusion and confusion with the two EDMX files? I dont know.

+6
source

Yes, there are performance differences between First Model (EF 4.0) and Code First (EF 4.1 - 4.3.1):

  • You cannot pre-generate internal query views . This means that every time EF tries to launch its first request (for the application domain), it will need to perform an expensive operation to create these views depending on the complexity of your model. This usually affects application startup performance and can make debugging pretty annoying.

  • You cannot use compiled queries . This can seriously affect frequently used queries, especially complex queries. This can become a serious bottleneck and processor swamp. Only EF 5.0 (which will be included in .NET 4.5) fixes this problem by automatically compiling all requests, including Code First requests.

+1
source

According to this benshmark yes . It also says that EF Model First is always faster than EF Code First.

0
source

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


All Articles