Files
TxSearchApi/Repositories/FwDbContext.cs
Roland Fieger 5e15db793b first things
2021-09-22 12:37:40 +02:00

75 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using Datamodels.Lookups;
using Datamodels.DatabaseModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace Repositories
{
public class FwDbContext: DbContext
{
public string _connectionString { get; set; } = "SERVER=127.0.0.1;DATABASE=testdb;PORT=3306;USER=root;PASSWORD=example";
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql(_connectionString);
}
public DbSet<LookupCategory> LookupCategories { get; set; }
public DbSet<LookupValue> LookupValues { get; set; }
public DbSet<Address> Address { get; set; }
public DbSet<Person> Person { get; set; }
public DbSet<Communication> Communication { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<LookupValue>()
.HasKey(x => x.Id);
modelBuilder.Entity<LookupValue>()
.Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<LookupCategory>()
.HasKey(x => x.Id);
modelBuilder.Entity<LookupCategory>()
.Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<LookupCategory>()
.HasMany(x => x.LookupValues)
.WithOne(y => y.LookupCategory);
modelBuilder.Entity<Person>()
.Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Person>()
.HasKey(x => x.Id);
modelBuilder.Entity<Person>()
.HasMany(x => x.Communications)
.WithOne(y => y.Person);
modelBuilder.Entity<Person>()
.HasMany(x => x.Addresses)
.WithOne(y => y.Person);
modelBuilder.Entity<Address>()
.Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Address>()
.HasKey(x => x.Id);
modelBuilder.Entity<Communication>()
.Property(x => x.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<Communication>()
.HasKey(x => x.Id);
}
}
}