Entity Framework 6源碼學習--設置調試EF環境


下載源代碼

打開https://github.com/aspnet/EntityFramework6下載源代碼。

 

建立調試解決方案

建立一個EntityFramework.Sample.sln在EntityFramework6根目錄,建一個Console工程在src目錄下,再將EF源代碼文件夾中的EntityFramework和EntityFramework.SqlServer加入到解決方案中。如下圖所示:

 

刪除延遲簽名

打開EntityFramework、EntityFramework.SqlServer項目屬性頁,發現簽名頁簽下面的”Delay sign only“被勾上,這樣生成的程序集項目無法進行調試,將Sign the assembly取消勾選。如下圖所示:

添加項目引用

在EntityFramework.Sample中添加對EntityFramework、EntityFramework.SqlServer這兩個項目的引用。如下圖所示:

 

建立測試數據庫

打開Microsoft SQL Server Management Studio,創建DB,名字為EFDB,然后選中新創建DB點擊菜單欄New Query。

USE [EFDB]
GO

/****** Object:  Table [dbo].[UserInfos]    Script Date: 6/15/2018 1:18:35 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[UserInfos](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Email] [nvarchar](max) NULL,
    [QQ] [nvarchar](max) NULL,
    [Position] [nvarchar](max) NOT NULL,
    [FirstLevel] [nvarchar](max) NOT NULL,
    [SecondLevel] [nvarchar](max) NOT NULL,
    [Mobile] [nvarchar](max) NULL,
 CONSTRAINT [PK_UserInfos] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

編寫測試代碼

在EntityFramework.Sample項目中修改Program.cs, 添加EFDBContext.cs和User.cs文件。

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFramework.Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new EFDBContext("Server=10.1.55.166;Database=EFDB;Password=1qaz2wsxE;User ID=sa"))
            {
                var users = context.Users.ToList();
                Console.WriteLine("{0,6}   {1,5}   {2,20}", "ID", "Name", "Email");
                foreach (var user in users)
                {
                    Console.WriteLine("{0,6}   {1,5}   {2,20}", user.Id, user.Name, user.Email);
                }
                Console.ReadKey();
            }
        }
    }
}

User.cs

using System.ComponentModel.DataAnnotations.Schema;

namespace EntityFramework.Sample
{
    [Table("UserInfos")]
    public partial class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string QQ { get; set; }
        public string Position { get; set; }
        public string FirstLevel { get; set; }
        public string SecondLevel { get; set; }
        public string Mobile { get; set; }
    }
}

EFDBContext.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFramework.Sample
{
    public class EFDBContext : DbContext
    {
        public EFDBContext(string connectionString)
            : base(connectionString)
        {
        }

        public IDbSet<User> Users { get; set; }
    }
}

開始調試

重新生成成功后,開始運行調試,成功進入EntityFramework源代碼斷點。 但是在運行時加載EntityFramework.SqlServer程序集失敗,雖然上面去掉強簽名,但是加載的時候仍然存在強簽名,最后Debug跟蹤,發現源碼中默認沒有給Provider會默認加載EntityFramework.SqlServer,並且是帶有強簽名的,只需要把這里的強簽名去掉即可,如下圖所示:

 

 

總結

設置調試環境,對於理解EntityFramework工作原理和學習源代碼尤其重要,但這不是目的,深入了解其代碼執行才是目標。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM