Probleme Compris besions d'initialiser context🚧

testTony
Tony Fages 1 year ago
parent 7afe33c430
commit 25712e7513

@ -5,6 +5,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization> <InvariantGlobalization>true</InvariantGlobalization>
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -12,12 +13,17 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1"/> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\API_DbDataManager\API_DbDataManager.csproj" /> <ProjectReference Include="..\API_DbDataManager\API_DbDataManager.csproj" />
<ProjectReference Include="..\API_Services\API_Services.csproj" /> <ProjectReference Include="..\API_Services\API_Services.csproj" />
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -17,6 +17,8 @@ builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<LibraryContext>(); builder.Services.AddDbContext<LibraryContext>();
builder.Services.AddScoped<IArticleService, DbManagerArticle>(); builder.Services.AddScoped<IArticleService, DbManagerArticle>();
builder.Services.AddScoped<IUserService, DbManagerUser>();
builder.Services.AddScoped<IFormulaireService, DbManagerFormulaire>();
var app = builder.Build(); var app = builder.Build();

@ -5,11 +5,22 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>DbDataManager</RootNamespace> <RootNamespace>DbDataManager</RootNamespace>
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\API_Services\API_Services.csproj" /> <ProjectReference Include="..\API_Services\API_Services.csproj" />
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" /> <ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -0,0 +1,12 @@
using DbContextLib;
namespace DbDataManager;
public class DbManager
{
protected LibraryContext _context;
public DbManager()
{
_context = new LibraryContext();
}
}

@ -11,7 +11,7 @@ public class DbManagerArticle : IArticleService
public DbManagerArticle(LibraryContext context) public DbManagerArticle(LibraryContext context)
{ {
_context = context; _context = new LibraryContext();
} }
public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime) public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime)
@ -34,6 +34,7 @@ public class DbManagerArticle : IArticleService
public async Task<Article?> DeleteArticle(long id) public async Task<Article?> DeleteArticle(long id)
{ {
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id); var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
Console.WriteLine(entity);
if (entity == null) return null; if (entity == null) return null;
_context.ArticleSet.Remove(entity); _context.ArticleSet.Remove(entity);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
@ -61,6 +62,7 @@ public class DbManagerArticle : IArticleService
public async Task<IEnumerable<Article?>> GetAllArticles() public async Task<IEnumerable<Article?>> GetAllArticles()
{ {
Console.WriteLine("GetAllArticles");
return await Task.FromResult(_context.ArticleSet.Select(a => a.ToModel()).AsEnumerable()); return await Task.FromResult(_context.ArticleSet.Select(a => a.ToModel()).AsEnumerable());
} }
} }

@ -10,7 +10,7 @@ using StubbedContextLib;
namespace StubbedContextLib.Migrations namespace StubbedContextLib.Migrations
{ {
[DbContext(typeof(StubbedContext))] [DbContext(typeof(StubbedContext))]
[Migration("20240305073325_mrg1")] [Migration("20240307182411_mrg1")]
partial class mrg1 partial class mrg1
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -149,7 +149,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("UserEntityId"); b.HasIndex("UserEntityId");
b.ToTable("FormEntity"); b.ToTable("FormSet");
}); });
modelBuilder.Entity("Entities.UserEntity", b => modelBuilder.Entity("Entities.UserEntity", b =>

@ -72,7 +72,7 @@ namespace StubbedContextLib.Migrations
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "FormEntity", name: "FormSet",
columns: table => new columns: table => new
{ {
Id = table.Column<long>(type: "INTEGER", nullable: false) Id = table.Column<long>(type: "INTEGER", nullable: false)
@ -85,9 +85,9 @@ namespace StubbedContextLib.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_FormEntity", x => x.Id); table.PrimaryKey("PK_FormSet", x => x.Id);
table.ForeignKey( table.ForeignKey(
name: "FK_FormEntity_UserSet_UserEntityId", name: "FK_FormSet_UserSet_UserEntityId",
column: x => x.UserEntityId, column: x => x.UserEntityId,
principalTable: "UserSet", principalTable: "UserSet",
principalColumn: "Id", principalColumn: "Id",
@ -132,8 +132,8 @@ namespace StubbedContextLib.Migrations
column: "UserEntityId"); column: "UserEntityId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_FormEntity_UserEntityId", name: "IX_FormSet_UserEntityId",
table: "FormEntity", table: "FormSet",
column: "UserEntityId"); column: "UserEntityId");
} }
@ -144,7 +144,7 @@ namespace StubbedContextLib.Migrations
name: "ArticleUserEntity"); name: "ArticleUserEntity");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "FormEntity"); name: "FormSet");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ArticleSet"); name: "ArticleSet");

@ -146,7 +146,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("UserEntityId"); b.HasIndex("UserEntityId");
b.ToTable("FormEntity"); b.ToTable("FormSet");
}); });
modelBuilder.Entity("Entities.UserEntity", b => modelBuilder.Entity("Entities.UserEntity", b =>

Loading…
Cancel
Save