Daten können hinzugefügt
This commit is contained in:
80
KanSan.Base/BaseRepository.cs
Normal file
80
KanSan.Base/BaseRepository.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using KanSan.Base.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace KanSan.Base
|
||||
{
|
||||
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
|
||||
{
|
||||
internal KanSanContext context;
|
||||
internal DbSet<TEntity> dbSet;
|
||||
|
||||
public BaseRepository(KanSanContext context)
|
||||
{
|
||||
this.context = context;
|
||||
if (context == null) throw new ArgumentNullException("context");
|
||||
this.dbSet = context.Set<TEntity>();
|
||||
}
|
||||
public virtual void Delete(TEntity entityToDelete)
|
||||
{
|
||||
if (context.Entry(entityToDelete).State == EntityState.Detached)
|
||||
dbSet.Attach(entityToDelete);
|
||||
dbSet.Remove(entityToDelete);
|
||||
}
|
||||
|
||||
public void Delete(object id)
|
||||
{
|
||||
TEntity entityToDelete = dbSet.Find(id);
|
||||
Delete(entityToDelete);
|
||||
}
|
||||
|
||||
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
|
||||
{
|
||||
IQueryable<TEntity> query = dbSet;
|
||||
|
||||
if (filter != null)
|
||||
{
|
||||
query = query.Where(filter);
|
||||
}
|
||||
|
||||
if (includeProperties != null)
|
||||
{
|
||||
foreach (var includeProperty in includeProperties.Split
|
||||
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
query = query.Include(includeProperty);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (orderBy != null)
|
||||
{
|
||||
return orderBy(query).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
return query.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public TEntity GetByID(object id)
|
||||
{
|
||||
return dbSet.Find(id);
|
||||
}
|
||||
|
||||
public virtual void Insert(TEntity entity)
|
||||
{
|
||||
dbSet.Add(entity);
|
||||
}
|
||||
|
||||
public void Update(TEntity entityToUpdate)
|
||||
{
|
||||
dbSet.Attach(entityToUpdate);
|
||||
context.Entry(entityToUpdate).State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
KanSan.Base/Interfaces/IRepository.cs
Normal file
21
KanSan.Base/Interfaces/IRepository.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace KanSan.Base.Interfaces
|
||||
{
|
||||
public interface IRepository<TEntity> where TEntity: class
|
||||
{
|
||||
void Delete(TEntity entityToDelete);
|
||||
void Delete(object id);
|
||||
IEnumerable<TEntity> Get(
|
||||
Expression<Func<TEntity, bool>> filter = null,
|
||||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
|
||||
string includeProperties = "");
|
||||
TEntity GetByID(object id);
|
||||
void Insert(TEntity entity);
|
||||
void Update(TEntity entityToUpdate);
|
||||
}
|
||||
}
|
||||
14
KanSan.Base/Interfaces/IUnitOfWork.cs
Normal file
14
KanSan.Base/Interfaces/IUnitOfWork.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using KanSan.Base.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace KanSan.Base.Interfaces
|
||||
{
|
||||
public interface IUnitOfWork
|
||||
{
|
||||
IRepository<Baustelle> BaustellenRepository { get; }
|
||||
IRepository<Kunde> KundenRepository { get; }
|
||||
void Commit();
|
||||
}
|
||||
}
|
||||
28
KanSan.Base/KanSan.Base.csproj
Normal file
28
KanSan.Base/KanSan.Base.csproj
Normal file
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.1" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.1.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="kansan.db">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
16
KanSan.Base/KanSanContext.cs
Normal file
16
KanSan.Base/KanSanContext.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using KanSan.Base.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
|
||||
namespace KanSan.Base
|
||||
{
|
||||
public class KanSanContext : DbContext
|
||||
{
|
||||
public DbSet<Baustelle> Baustellen { get; set; }
|
||||
public DbSet<Kunde> Kunden { get; set; }
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseSqlite("Data Source=kansan.db");
|
||||
}
|
||||
}
|
||||
}
|
||||
87
KanSan.Base/Migrations/20200220200339_InitialCommit.Designer.cs
generated
Normal file
87
KanSan.Base/Migrations/20200220200339_InitialCommit.Designer.cs
generated
Normal file
@@ -0,0 +1,87 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using KanSan.Base;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace KanSan.Base.Migrations
|
||||
{
|
||||
[DbContext(typeof(KanSanContext))]
|
||||
[Migration("20200220200339_InitialCommit")]
|
||||
partial class InitialCommit
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "3.1.1");
|
||||
|
||||
modelBuilder.Entity("KanSan.Base.Models.Baustelle", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("GuidNr")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("KundeID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Ort")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Projektnummer")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Strasse")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.HasIndex("KundeID");
|
||||
|
||||
b.ToTable("Baustellen");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("KanSan.Base.Models.Kunde", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("GuidNr")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Nachname")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Ort")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("PLZ")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Strasse")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Vorname")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.ToTable("Kunden");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("KanSan.Base.Models.Baustelle", b =>
|
||||
{
|
||||
b.HasOne("KanSan.Base.Models.Kunde", "Kunde")
|
||||
.WithMany("Baustellen")
|
||||
.HasForeignKey("KundeID");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
66
KanSan.Base/Migrations/20200220200339_InitialCommit.cs
Normal file
66
KanSan.Base/Migrations/20200220200339_InitialCommit.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace KanSan.Base.Migrations
|
||||
{
|
||||
public partial class InitialCommit : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Kunden",
|
||||
columns: table => new
|
||||
{
|
||||
ID = table.Column<int>(nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
GuidNr = table.Column<Guid>(nullable: false),
|
||||
Vorname = table.Column<string>(nullable: true),
|
||||
Nachname = table.Column<string>(nullable: true),
|
||||
Strasse = table.Column<string>(nullable: true),
|
||||
PLZ = table.Column<string>(nullable: true),
|
||||
Ort = table.Column<string>(nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Kunden", x => x.ID);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Baustellen",
|
||||
columns: table => new
|
||||
{
|
||||
ID = table.Column<int>(nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
GuidNr = table.Column<Guid>(nullable: false),
|
||||
KundeID = table.Column<int>(nullable: true),
|
||||
Ort = table.Column<string>(nullable: true),
|
||||
Strasse = table.Column<string>(nullable: true),
|
||||
Projektnummer = table.Column<string>(nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Baustellen", x => x.ID);
|
||||
table.ForeignKey(
|
||||
name: "FK_Baustellen_Kunden_KundeID",
|
||||
column: x => x.KundeID,
|
||||
principalTable: "Kunden",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Baustellen_KundeID",
|
||||
table: "Baustellen",
|
||||
column: "KundeID");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Baustellen");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Kunden");
|
||||
}
|
||||
}
|
||||
}
|
||||
85
KanSan.Base/Migrations/KanSanContextModelSnapshot.cs
Normal file
85
KanSan.Base/Migrations/KanSanContextModelSnapshot.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using KanSan.Base;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace KanSan.Base.Migrations
|
||||
{
|
||||
[DbContext(typeof(KanSanContext))]
|
||||
partial class KanSanContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "3.1.1");
|
||||
|
||||
modelBuilder.Entity("KanSan.Base.Models.Baustelle", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("GuidNr")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("KundeID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Ort")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Projektnummer")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Strasse")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.HasIndex("KundeID");
|
||||
|
||||
b.ToTable("Baustellen");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("KanSan.Base.Models.Kunde", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("GuidNr")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Nachname")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Ort")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("PLZ")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Strasse")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Vorname")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.ToTable("Kunden");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("KanSan.Base.Models.Baustelle", b =>
|
||||
{
|
||||
b.HasOne("KanSan.Base.Models.Kunde", "Kunde")
|
||||
.WithMany("Baustellen")
|
||||
.HasForeignKey("KundeID");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
14
KanSan.Base/Models/Baustelle.cs
Normal file
14
KanSan.Base/Models/Baustelle.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace KanSan.Base.Models
|
||||
{
|
||||
public class Baustelle : DatenbankClass
|
||||
{
|
||||
public Kunde Kunde { get; set; }
|
||||
public string Ort { get; set; }
|
||||
public string Strasse { get; set; }
|
||||
public string Projektnummer { get; set; }
|
||||
}
|
||||
}
|
||||
12
KanSan.Base/Models/DatenbankClass.cs
Normal file
12
KanSan.Base/Models/DatenbankClass.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace KanSan.Base.Models
|
||||
{
|
||||
public class DatenbankClass
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public Guid GuidNr { get; set; }
|
||||
}
|
||||
}
|
||||
17
KanSan.Base/Models/Kunde.cs
Normal file
17
KanSan.Base/Models/Kunde.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace KanSan.Base.Models
|
||||
{
|
||||
public class Kunde : DatenbankClass
|
||||
{
|
||||
public string Vorname { get; set; }
|
||||
public string Nachname { get; set; }
|
||||
public string Strasse { get; set; }
|
||||
public string PLZ { get; set; }
|
||||
public string Ort { get; set; }
|
||||
public List<Baustelle> Baustellen { get; } = new List<Baustelle>();
|
||||
|
||||
}
|
||||
}
|
||||
41
KanSan.Base/UnitOfWork.cs
Normal file
41
KanSan.Base/UnitOfWork.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using KanSan.Base.Interfaces;
|
||||
using KanSan.Base.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace KanSan.Base
|
||||
{
|
||||
public class UnitOfWork : IUnitOfWork
|
||||
{
|
||||
private KanSanContext _dbContext;
|
||||
private BaseRepository<Baustelle> _baustellen;
|
||||
private BaseRepository<Kunde> _kunden;
|
||||
|
||||
public UnitOfWork(KanSanContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public IRepository<Baustelle> BaustellenRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
return _baustellen ?? (_baustellen = new BaseRepository<Baustelle>(_dbContext));
|
||||
}
|
||||
}
|
||||
|
||||
public IRepository<Kunde> KundenRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
return _kunden ?? (_kunden = new BaseRepository<Kunde>(_dbContext));
|
||||
}
|
||||
}
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user