How to debug Entity Framework 6 source code

Source: Internet
Author: User
Tags code tag
0 Abstract

This article describes how to debug the source code of Entity Framework 6 (EF 6) in Visual Studio 2013 (vs 2013. In principle, vs 2012 is also applicable.

This code ant was planning to write a series of blog posts in "Entity Framework 6 Source Code debugging and analysis". due to personal health reasons, it takes a long time to take a rest and has to give up this plan. Before leaving, write the method for debugging the EF source code as a tutorial, hoping to help new users like me.

1 Text

Software Environment

Operating System: Windows 8.1 update 1

IDE: Visual Studio 2013 update 2

 

0 download EF 6 source code

The latest source code is EF 6.1.0. You need git to obtain the code. This article does not discuss how to install and configure git in windows.

Find the source code tag on the EF homepage and click Clone to get the GIT clone address https://git01.codeplex.com/entityframework, as shown in.

 

Use git to obtain the EF source code, as shown in.

Now, you can directly open entityframework. sln to debug the EF source code. Next we will add the EF source code to our project for debugging in the form of a new solution.

 

1. Create a console Project

Create a C # console project in vs. the. NET version can be 4.5 or 4.5.1. We recommend that you select 4.5 as the project name, as shown in. Other project types, such as MVC, are also acceptable, depending on your preferences.

 

2. Add entityframework and entityframework. sqlserver to solution.

By adding an existing project to the solution, we get the reference of the two projects in the title (the specific path stores the EF source code for you), as shown in.

Add a reference to the entityframework. sqlserver project in the same way. The result is shown in.

Add references to the two projects in the efsrctest project.

 

3 compileEntityframework and entityframework. sqlserver Project

There are two methods in this step:

First, modify the output paths of the two projects to the DEBUG directory of the console project you created or the directory you specified;

The second is to compile and copy the DLL to the debug or specified directory of your project. The disadvantage is that you have modified the EF source code, after re-compilation, you must copy the DLL to the specified directory again to make it take effect. However, beginners like me are unlikely to modify the EF source code, so follow method 2 to continue.

Compile the console project, copy the DLL and PDB file output by the entityframework and entityframework. sqlserver project to the DEBUG directory of the console project (or the directory you specified, which will not be described here), as shown in.

Now, all the dynamic link library files required for debugging are ready, and the next step is the last step.

 

4. Configure the app. config file of the console Project

Code first is used here. The configuration content is different from mode first and database first, mainly because of the difference in connection strings.

 1 <?xml version="1.0" encoding="utf-8"?> 2  3 <configuration> 4   <configSections> 5     <section name="entityFramework" 6              type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 7              requirePermission="false" /> 8   </configSections> 9   <startup>10     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />11   </startup>12   <entityFramework>13     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">14       <parameters>15         <parameter value="v11.0" />16       </parameters>17     </defaultConnectionFactory>18     <providers>19       <provider invariantName="System.Data.SqlClient"20                 type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />21     </providers>22   </entityFramework>23 </configuration>

The Section node is critical. if the version is incorrect, an error is returned.

So far, the configuration has been completed. Finally, write several simple classes for debugging.

 

5. Create a database using code first

Create a database that only contains one table. The Code is as follows:

1 namespace EFSrcTest2 {3     class Teacher4     {5         public int ID { get; set; }6         public string FirstName { get; set; }7     }8 }

 

1 namespace EFSrcTest2 {3     class SchoolContext : DbContext4     {5         public DbSet<Teacher> Teachers { get; set; }6     }7 }

 

1 namespace efsrctest 2 {3 class program 4 {5 static void main (string [] ARGs) 6 {7 using (var ctx = new schoolcontext () 8 {9 CTX. teachers. add (new teacher {firstname = ""}); 10 CTX. savechanges (); 11} 12} 13} 14}

Now, place a breakpoint in the 7th line using (var ctx = new schoolcontext () and press F11 to open the EF source code, as shown in.

Now, let's start debugging your Entity Framework 6 source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.