DbContext在其生命周期中是否保持打开的连接?

时间:2018-11-13 09:34:36

标签: c# entity-framework lifecycle dbconnection

问题很清楚。 # Game of life from random import randint import numpy as np from copy import deepcopy from enum import Enum import tkinter as tk class State(Enum): Dead = 0 Alive = 1 def __str__(self): return str(self.value) @property def color(self): return 'blue' if self.value else 'red' class Cell: def __init__(self, m, n, state): self.m = np.uint(m) self.n = np.uint(n) self.state = state def kill(self): self.state = State.Dead def birth(self): self.state = State.Alive def __str__(self): return '({},{}) {}'.format(self.m, self.n, self.state) def __repr__(self): return '({},{}) {}'.format(self.m, self.n, self.state) class Game: CELL_TAG = 'cells' def __init__(self, m, n, alive_cells = None): self.m = m self.n = n self.grid = np.ndarray((m,n), dtype = np.uint8) if alive_cells: self.cells = [Cell(i // n, i % n, State.Alive if (i // n, i % n) in alive_cells else State.Dead) for i in range(m*n)] else: self.cells = [Cell(i / n, i % n, randint(0,1)) for i in range(m*n)] # GUI # self.top = tk.Tk() self.cell_size = 10000 // 400 # (self.m * self.n) self.canvas = tk.Canvas(self.top, bg="gray", height=self.m * self.cell_size, width=self.n * self.cell_size) self.canvas.pack() def populate_grid(self): for cell in self.cells: self.grid[cell.m, cell.n] = cell.state.value def show(self, show_GUI = True, print_2_console = False): self.populate_grid() if print_2_console: print('#'*self.m*3) print(self.grid) if show_GUI: self.draw_canvas() self.top.mainloop() def iterate(self): ''' Rules: (1) If cell has less than 2 neighbours, it dies (2) If cell has more than 3 neighbours, it dies (3) If cell has 2-3 neighbours, it survives (4) If cell has 3 neighbours, it rebirths ''' new_cells = [] for cell in self.cells: alive_neighbours = 0 for i in range(cell.m - 1, cell.m + 2): for j in range(cell.n - 1, cell.n + 2): if i == cell.m and j == cell.n: continue else: try: alive_neighbours += self.grid[i,j] except IndexError: pass tmp = deepcopy(cell) if alive_neighbours < 2 or alive_neighbours > 3: tmp.kill() elif alive_neighbours == 3: tmp.birth() else: # == 2 pass new_cells.append(tmp) self.cells = new_cells self.populate_grid() self.draw_canvas() def draw_canvas(self): self.canvas.delete(self.CELL_TAG) # Gets rid of any existing cell rects. for cell in self.cells: self.canvas.create_rectangle( cell.n*self.cell_size, cell.m*self.cell_size, (1+cell.n)*self.cell_size, (1+cell.m)*self.cell_size, fill=cell.state.color, tag=self.CELL_TAG) self.top.after(100, self.iterate) if __name__ == "__main__": glider = (20, 20, ((1,3), (2,3), (2,1), (3,2), (3,3))) small_exploder = (30, 30, ((10,10), (11,9), (11,10), (11,11), (12,9), (12,11), (13,10))) M, N, STARTING_LIVE_CELLS, ITERATIONS = *small_exploder, 0 g = Game(M, N, STARTING_LIVE_CELLS) g.show() 在其生命周期中是否保持打开连接? EF核心呢?

1 个答案:

答案 0 :(得分:2)

正如其他人所指出的,除非您手动打开连接并将其传递给DbContext构造函数,否则不会。

具体的详细答案可以在https://stackoverflow.com/a/45330219/191148处找到。

@ajcvickers在https://github.com/aspnet/EntityFrameworkCore/issues/7810中的评论将其清除:

  

如果EF创建了DbConnection对象,则EF将确保它是   在处置DbContext时处置。另一方面,如果有的话   其他代码创建DbConnection对象,并将其传递给EF,然后   其他代码也有责任处置   适当的连接。

     

类似的规则适用于打开和关闭连接。如果EF打开   连接,然后EF将在完成连接后关闭连接   它。如果您的代码打开了连接,则您的代码应关闭   连接。