Added ef core integration
This commit is contained in:
20
debug/TestApplication/DatabaseContext.cs
Normal file
20
debug/TestApplication/DatabaseContext.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TestApplication.Models;
|
||||
|
||||
namespace TestApplication;
|
||||
|
||||
public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbContext(options) {
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
public DbSet<Post> Posts { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.HasMany(u => u.Posts)
|
||||
.WithOne(p => p.Sender)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
13
debug/TestApplication/Models/Post.cs
Normal file
13
debug/TestApplication/Models/Post.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TestApplication.Models;
|
||||
|
||||
public class Post {
|
||||
[Key]
|
||||
public Guid Id { get; } = Guid.CreateVersion7();
|
||||
|
||||
public required User Sender { get; set; }
|
||||
|
||||
[MaxLength(5000)]
|
||||
public required string Message { get; set; }
|
||||
}
|
||||
30
debug/TestApplication/Models/User.cs
Normal file
30
debug/TestApplication/Models/User.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TestApplication.Models;
|
||||
|
||||
public class User {
|
||||
[Key]
|
||||
public Guid Id { get; } = Guid.CreateVersion7();
|
||||
|
||||
[EmailAddress, MaxLength(25)]
|
||||
public required string Email { get; set; }
|
||||
|
||||
[MaxLength(25)]
|
||||
public required string Username { get; set; }
|
||||
|
||||
[MaxLength(64)]
|
||||
public required string Password { get; set; }
|
||||
|
||||
[MaxLength(25)]
|
||||
public required string FirstName { get; set; }
|
||||
|
||||
[MaxLength(25)]
|
||||
public required string LastName { get; set; }
|
||||
|
||||
[MaxLength(255)]
|
||||
public string? Description { get; set; }
|
||||
|
||||
public required DateOnly Birth { get; set; } = DateOnly.FromDateTime(DateTime.Today);
|
||||
|
||||
public List<Post> Posts { get; set; } = new();
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
using HopFrame.Core;
|
||||
using HopFrame.Core.EFCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TestApplication;
|
||||
using TestApplication.Components;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -6,6 +10,15 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents();
|
||||
|
||||
builder.Services.AddEntityFrameworkInMemoryDatabase();
|
||||
builder.Services.AddDbContext<DatabaseContext>(options => {
|
||||
options.UseInMemoryDatabase("testing");
|
||||
});
|
||||
|
||||
builder.Services.AddHopFrame(config => {
|
||||
config.AddDbContext<DatabaseContext>();
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5281",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
@@ -13,7 +13,7 @@
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7126;http://localhost:5281",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
|
||||
@@ -11,4 +11,8 @@
|
||||
<ProjectReference Include="..\..\src\HopFrame.Core\HopFrame.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user