码迷,mamicode.com
首页 > 数据库 > 详细

EF Core 2.0中开启Transaction事务会对DbContext创建和关闭数据库连接的行为有所影响

时间:2018-10-17 12:16:25      阅读:1043      评论:0      收藏:0      [点我收藏+]

标签:img   generic   constrain   ati   arch   red   table   clust   nbsp   

我们先在数据库中建立一个Book表:

CREATE TABLE [dbo].[Book](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [BookName] [nvarchar](50) NULL,
    [BookDescription] [nvarchar](50) NULL,
    [ISBN] [nvarchar](20) NULL,
    [CreateTime] [datetime] NULL,
 CONSTRAINT [PK_Book] 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]
GO

ALTER TABLE [dbo].[Book] ADD  CONSTRAINT [DF_Book_CreateTime]  DEFAULT (getdate()) FOR [CreateTime]
GO

然后插入如下数据:

INSERT [dbo].[Book] ([BookName], [BookDescription], [ISBN]) VALUES (NChinese, NChinese, N0001)
GO
INSERT [dbo].[Book] ([BookName], [BookDescription], [ISBN]) VALUES (NEnglish, NEnglish, N0002)
GO
INSERT [dbo].[Book] ([BookName], [BookDescription], [ISBN]) VALUES (NJapanese, NJapanese, N0003)
GO
INSERT [dbo].[Book] ([BookName], [BookDescription], [ISBN]) VALUES (NRussian, NRussian, N0004)
GO
INSERT [dbo].[Book] ([BookName], [BookDescription], [ISBN]) VALUES (NItalian, NItalian, N0005)
GO

查询Book表的数据,如下图所示:

技术分享图片

 

现在我们使用EF Core将Book表映射到.NET Core控制台项目中的Book实体上,Book实体如下所示:

using System;
using System.Collections.Generic;

namespace EFCoreDB.Entities
{
    public partial class Book
    {
        public int Id { get; set; }
        public string BookName { get; set; }
        public string BookDescription { get; set; }
        public string Isbn { get; set; }
        public DateTime? CreateTime { get; set; }
    }
}

 

EF Core 2.0中开启Transaction事务会对DbContext创建和关闭数据库连接的行为有所影响

标签:img   generic   constrain   ati   arch   red   table   clust   nbsp   

原文地址:https://www.cnblogs.com/OpenCoder/p/9803032.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!