parent
7557bcae1a
commit
a54a2676f2
@ -0,0 +1,33 @@
|
|||||||
|
reprise de l'exemple 041_001 avec seulement SqlServer
|
||||||
|
nuget, dotnet restore...
|
||||||
|
|
||||||
|
modification de OnConfiguring
|
||||||
|
pour avoir un provider par défaut, mais la possibilité d'en injecter un autre via un constructeur
|
||||||
|
le constructeur par défaut permettra d'utiliser la version par défaut
|
||||||
|
on utilise pour ça la propriété IsConfigured
|
||||||
|
```csharp
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||||
|
{
|
||||||
|
if (!options.IsConfigured)
|
||||||
|
{
|
||||||
|
options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=ex_041_004_InMemory.Nounours.mdf;Trusted_Connection=True;");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
ajout des constructeurs
|
||||||
|
```csharp
|
||||||
|
public SqlServerContext()
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public SqlServerContext(DbContextOptions<SqlServerContext> options)
|
||||||
|
: base(options)
|
||||||
|
{ }
|
||||||
|
```
|
||||||
|
|
||||||
|
test
|
||||||
|
* nouveau projet
|
||||||
|
* dépendances
|
||||||
|
* nuget
|
||||||
|
* écriture du test
|
@ -0,0 +1,12 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0"/>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
@ -0,0 +1,39 @@
|
|||||||
|
using ex_041_004_InMemory;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ex_041_004_UnitTests
|
||||||
|
{
|
||||||
|
public class NounoursDB_Tests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Add_Test()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<SqlServerContext>()
|
||||||
|
.UseInMemoryDatabase(databaseName: "Add_writes_to_database")
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
// Run the test against one instance of the context
|
||||||
|
using (var context = new SqlServerContext(options))
|
||||||
|
{
|
||||||
|
Nounours chewie = new Nounours { Nom = "Chewbacca" };
|
||||||
|
Nounours yoda = new Nounours { Nom = "Yoda" };
|
||||||
|
Nounours ewok = new Nounours { Nom = "Ewok" };
|
||||||
|
|
||||||
|
context.Nounours.Add(chewie);
|
||||||
|
context.Nounours.Add(yoda);
|
||||||
|
context.Nounours.Add(ewok);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use a separate instance of the context to verify correct data was saved to database
|
||||||
|
using (var context = new SqlServerContext(options))
|
||||||
|
{
|
||||||
|
Assert.Equal(3, context.Nounours.Count());
|
||||||
|
Assert.Equal("Chewbacca", context.Nounours.First().Nom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue