data access for agent
parent
584525cf87
commit
3264ef6a24
|
@ -1,8 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace NetworkResurrector.Agent.Domain.Data
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using NetworkResurrector.Agent.Domain.Data.EntityTypeConfiguration;
|
||||
using NetworkResurrector.Agent.Domain.Entities;
|
||||
|
||||
namespace NetworkResurrector.Agent.Domain.Data.DbContexts
|
||||
{
|
||||
public class AgentDbContext : DbContext
|
||||
{
|
||||
public DbSet<Machine> Machines { get; set; }
|
||||
|
||||
public AgentDbContext(DbContextOptions<AgentDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
base.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
|
||||
base.ChangeTracker.AutoDetectChangesEnabled = true;
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.ApplyConfiguration(new MachineConfiguration());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NetworkResurrector.Agent.Domain.Data.DbContexts;
|
||||
using NetworkResurrector.Agent.Domain.Data.Repositories;
|
||||
using NetworkResurrector.Agent.Domain.Repositories;
|
||||
|
||||
namespace NetworkResurrector.Agent.Domain.Data
|
||||
{
|
||||
public static class DependencyInjectionExtensions
|
||||
{
|
||||
public static void AddDataAccess(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IAgentRepository, AgentRepository>();
|
||||
|
||||
services
|
||||
.AddDbContextPool<AgentDbContext>(
|
||||
(serviceProvider, options) =>
|
||||
{
|
||||
var configuration = serviceProvider.GetService<IConfiguration>();
|
||||
var connectionString = configuration.GetConnectionString("DatabaseConnection");
|
||||
options.UseSqlServer(connectionString);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using NetworkResurrector.Agent.Domain.Entities;
|
||||
|
||||
namespace NetworkResurrector.Agent.Domain.Data.EntityTypeConfiguration
|
||||
{
|
||||
class MachineConfiguration : IEntityTypeConfiguration<Machine>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Machine> builder)
|
||||
{
|
||||
builder.ToTable("Machine").HasKey(key => key.MachineId);
|
||||
builder.Property(z => z.MachineId).ValueGeneratedOnAdd();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,4 +4,12 @@
|
|||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EntityFrameworkCorePackageVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NetworkResurrector.Agent.Domain\NetworkResurrector.Agent.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using NetworkResurrector.Agent.Domain.Data.DbContexts;
|
||||
using NetworkResurrector.Agent.Domain.Entities;
|
||||
using NetworkResurrector.Agent.Domain.Repositories;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NetworkResurrector.Agent.Domain.Data.Repositories
|
||||
{
|
||||
class AgentRepository : IAgentRepository
|
||||
{
|
||||
private readonly AgentDbContext _dbContext;
|
||||
|
||||
public AgentRepository(AgentDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public Task<Machine[]> GetMachines()
|
||||
{
|
||||
return _dbContext.Machines.ToArrayAsync();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
if not exists (select top 1 1 from sys.objects where name = 'Machine' and type = 'U')
|
||||
begin
|
||||
create table Machine
|
||||
(
|
||||
MachineId int identity(0, 1) constraint PK_Machine primary key,
|
||||
MachineName varchar(50) not null,
|
||||
FullMachineName varchar(100) not null,
|
||||
MACAddress varchar(20) not null,
|
||||
IPv4Address varchar(20),
|
||||
[Description] varchar(max)
|
||||
)
|
||||
end
|
||||
go
|
||||
|
||||
if not exists (select top 1 1 from Machine)
|
||||
begin
|
||||
insert into Machine(MachineName, FullMachineName, MACAddress, IPv4Address, [Description])
|
||||
select '***REMOVED***', '***REMOVED***', '***REMOVED***', '***REMOVED***', 'Personal main development station' union
|
||||
select '***REMOVED***', '***REMOVED***', '***REMOVED***', null, 'Main file server'
|
||||
end
|
||||
go
|
|
@ -1,8 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace NetworkResurrector.Agent.Domain
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
namespace NetworkResurrector.Agent.Domain.Entities
|
||||
{
|
||||
public class Machine
|
||||
{
|
||||
public int MachineId { get; set; }
|
||||
public string MachineName { get; set; }
|
||||
public string FullMachineName { get; set; }
|
||||
public string MACAddress { get; set; }
|
||||
public string IPv4Address { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using NetworkResurrector.Agent.Domain.Entities;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NetworkResurrector.Agent.Domain.Repositories
|
||||
{
|
||||
public interface IAgentRepository
|
||||
{
|
||||
Task<Machine[]> GetMachines();
|
||||
}
|
||||
}
|
|
@ -1,7 +1,11 @@
|
|||
{
|
||||
"urls": "http://*:5064",
|
||||
"ConnectionStrings": {
|
||||
"DatabaseConnection": "***REMOVED***"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Default": "Debug",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue