-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityFrameworkSqlServerExtensions.cs
More file actions
57 lines (48 loc) · 2.73 KB
/
EntityFrameworkSqlServerExtensions.cs
File metadata and controls
57 lines (48 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//-----------------------------------------------------------------------
// <copyright file="EntityFrameworkSqlServerExtensions.cs" company="P.O.S Informatique">
// Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace PosInformatique.Testing.Databases.SqlServer
{
using Microsoft.EntityFrameworkCore;
/// <summary>
/// Extensions methods of the <see cref="SqlServer"/> class with additional
/// helpers for Entity Framework.
/// </summary>
public static class EntityFrameworkSqlServerExtensions
{
/// <summary>
/// Creates a database using the specified Entity Framework <paramref name="context"/>.
/// </summary>
/// <remarks>If the database already exists, it will be deleted.</remarks>
/// <param name="server"><see cref="SqlServer"/> instance where the database will be created.</param>
/// <param name="name">Name of the database to create.</param>
/// <param name="context"><see cref="DbContext"/> which represents the database to create.</param>
/// <returns>An instance of the <see cref="SqlServerDatabase"/> which represents the deployed database.</returns>
public static SqlServerDatabase CreateDatabase(this SqlServer server, string name, DbContext context)
{
var database = server.GetDatabase(name);
context.Database.SetConnectionString(database.ConnectionString);
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
return database;
}
/// <summary>
/// Creates a database using the specified Entity Framework <paramref name="context"/>.
/// </summary>
/// <remarks>If the database already exists, it will be deleted.</remarks>
/// <param name="server"><see cref="SqlServer"/> instance where the database will be created.</param>
/// <param name="name">Name of the database to create.</param>
/// <param name="context"><see cref="DbContext"/> which represents the database to create.</param>
/// <returns>A <see cref="Task"/> which represents the asynchronous operation and contains an instance of the <see cref="SqlServerDatabase"/> that represents the deployed database.</returns>
public static async Task<SqlServerDatabase> CreateDatabaseAsync(this SqlServer server, string name, DbContext context)
{
var database = server.GetDatabase(name);
context.Database.SetConnectionString(database.ConnectionString);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
return database;
}
}
}