mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-02-22 22:55:01 +01:00
[DB] DbContext relations
This commit is contained in:
parent
0f6b2fec88
commit
2938e1311f
|
@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DotBased.AspNet.Authority.EFCore;
|
namespace DotBased.AspNet.Authority.EFCore;
|
||||||
|
|
||||||
public class AuthorityContext : DbContext
|
public class AuthorityContext(DbContextOptions<AuthorityContext> options) : DbContext(options)
|
||||||
{
|
{
|
||||||
public DbSet<AuthorityAttribute> Attributes { get; set; }
|
public DbSet<AuthorityAttribute> Attributes { get; set; }
|
||||||
public DbSet<AuthorityGroup> Groups { get; set; }
|
public DbSet<AuthorityGroup> Groups { get; set; }
|
||||||
|
@ -12,10 +12,32 @@ public class AuthorityContext : DbContext
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
modelBuilder.Entity<AuthorityAttribute>().ToTable("authority_attributes");
|
modelBuilder.Entity<AuthorityAttribute>(attributeEntity =>
|
||||||
modelBuilder.Entity<AuthorityGroup>().ToTable("authority_groups");
|
{
|
||||||
modelBuilder.Entity<AuthorityRole>().ToTable("authority_roles");
|
attributeEntity.ToTable("authority_attributes");
|
||||||
modelBuilder.Entity<AuthorityUser>().ToTable("authority_users");
|
attributeEntity.HasKey(a => new { a.BoundId, a.AttributeKey });
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<AuthorityGroup>(groupEntity =>
|
||||||
|
{
|
||||||
|
groupEntity.ToTable("authority_groups");
|
||||||
|
groupEntity.HasKey(x => x.Id);
|
||||||
|
groupEntity.HasMany(g => g.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<AuthorityRole>(roleEntity =>
|
||||||
|
{
|
||||||
|
roleEntity.ToTable("authority_roles");
|
||||||
|
roleEntity.HasKey(x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<AuthorityUser>(userEntity =>
|
||||||
|
{
|
||||||
|
userEntity.ToTable("authority_users");
|
||||||
|
userEntity.HasKey(x => x.Id);
|
||||||
|
userEntity.HasMany(u => u.Attributes).WithOne().HasForeignKey(a => a.BoundId).OnDelete(DeleteBehavior.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
base.OnModelCreating(modelBuilder);
|
base.OnModelCreating(modelBuilder);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -12,6 +12,10 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.12" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.12" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.12">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.12" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.12" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
23
DotBased.AspNet.Authority.EFCore/README.md
Normal file
23
DotBased.AspNet.Authority.EFCore/README.md
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# EF Core database
|
||||||
|
|
||||||
|
## Add migration project
|
||||||
|
```csharp
|
||||||
|
options.UseSqlite("Data Source=dev-dotbased.db", c => c.MigrationsAssembly("PROJECT-NAME"));
|
||||||
|
```
|
||||||
|
|
||||||
|
## EF Tool
|
||||||
|
|
||||||
|
Add migration
|
||||||
|
```shell
|
||||||
|
dotnet ef migrations add MIGRATION-NAME --project PROJECT-NAME
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove migrations
|
||||||
|
```shell
|
||||||
|
dotnet ef migrations remove --project PROJECT-NAME
|
||||||
|
```
|
||||||
|
|
||||||
|
Update database
|
||||||
|
```shell
|
||||||
|
dotnet ef database update --project PROJECT-NAME
|
||||||
|
```
|
|
@ -17,6 +17,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Folder Include="Models\Data\" />
|
||||||
<Folder Include="Models\Security\" />
|
<Folder Include="Models\Security\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -1,26 +1,18 @@
|
||||||
namespace DotBased.AspNet.Authority.Models.Authority;
|
namespace DotBased.AspNet.Authority.Models.Authority;
|
||||||
|
|
||||||
public class AuthorityAttribute
|
public class AuthorityAttribute(string attributeKey, Guid bound)
|
||||||
{
|
{
|
||||||
public AuthorityAttribute(string attributeKey, string bound)
|
public AuthorityAttribute() : this(string.Empty, Guid.NewGuid())
|
||||||
{
|
{
|
||||||
AttributeKey = attributeKey;
|
|
||||||
BoundId = bound;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public AuthorityAttribute()
|
public Guid BoundId { get; set; } = bound;
|
||||||
{
|
|
||||||
AttributeKey = string.Empty;
|
|
||||||
BoundId = string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string AttributeKey { get; set; } // ClaimType/Authority.attribute.enabled
|
|
||||||
|
|
||||||
public string BoundId { get; set; } // Bound to User, Group, Role id
|
public string AttributeKey { get; set; } = attributeKey;
|
||||||
|
|
||||||
public object? AttributeValue { get; set; }
|
public string AttributeValue { get; set; } = string.Empty;
|
||||||
|
|
||||||
public string? Type { get; set; } // AspNet.Claim.Role/Property/Data.JSON, Data.Raw, Data.Base64 etc.
|
public string? Type { get; set; }
|
||||||
|
|
||||||
public long Version { get; set; }
|
public long Version { get; set; }
|
||||||
}
|
}
|
|
@ -20,4 +20,5 @@ public class AuthorityGroup
|
||||||
public long Version { get; set; }
|
public long Version { get; set; }
|
||||||
|
|
||||||
public DateTime CreatedDate { get; set; }
|
public DateTime CreatedDate { get; set; }
|
||||||
|
public ICollection<AuthorityAttribute> Attributes { get; set; } = [];
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
namespace DotBased.AspNet.Authority.Models.Authority;
|
namespace DotBased.AspNet.Authority.Models.Authority;
|
||||||
|
|
||||||
public abstract class AuthorityRole()
|
public class AuthorityRole()
|
||||||
{
|
{
|
||||||
public AuthorityRole(string name) : this()
|
public AuthorityRole(string name) : this()
|
||||||
{
|
{
|
||||||
|
|
|
@ -41,5 +41,7 @@ public class AuthorityUser()
|
||||||
|
|
||||||
public bool PhoneNumberConfirmed { get; set; }
|
public bool PhoneNumberConfirmed { get; set; }
|
||||||
|
|
||||||
|
public ICollection<AuthorityAttribute> Attributes { get; set; } = [];
|
||||||
|
|
||||||
public override string ToString() => UserName ?? EmailAddress ?? string.Empty;
|
public override string ToString() => UserName ?? EmailAddress ?? string.Empty;
|
||||||
}
|
}
|
|
@ -23,7 +23,7 @@ builder.Logging.ClearProviders();
|
||||||
builder.Logging.AddDotBasedLoggerProvider(LogService.Options);
|
builder.Logging.AddDotBasedLoggerProvider(LogService.Options);
|
||||||
builder.Services.AddAuthorityContext(options =>
|
builder.Services.AddAuthorityContext(options =>
|
||||||
{
|
{
|
||||||
options.UseSqlite("Data Source=dev-dotbased.db");
|
options.UseSqlite("Data Source=dev-dotbased.db", c => c.MigrationsAssembly("TestWebApi"));
|
||||||
});
|
});
|
||||||
builder.Services.AddAuthority(options =>
|
builder.Services.AddAuthority(options =>
|
||||||
{
|
{
|
||||||
|
|
|
@ -8,7 +8,11 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8"/>
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8"/>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.1" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.12">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.12" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user