Commit 6023333a authored by Andres Käver's avatar Andres Käver
Browse files

work

parent d89b6470
......@@ -13,35 +13,45 @@ namespace ConsoleApp1
Console.WriteLine("Hello EF Core test!");
var ctx = new AppDbContext();
/*
var ctype = ctx.ContactTypes
.SingleOrDefault(cc => cc.ContactTypeValue == "Skype") ??
new ContactType() { ContactTypeValue = "Skype" };
var p = new Person() { FirstName = "Mati", LastName = "Kaal" };
var ct = new ContactType() { ContactTypeValue = "Skype" };
var c = new Contact()
{
Person = p,
ContactType = ct,
ContactType = ctype,
ContactValue = "MatiKaal"
};
ctx.Contacts.Add(c);
ctx.SaveChanges();
/*
foreach (var ctxContactType in ctx.ContactTypes.Include(cc => cc.Contacts))
{
ctx.Contacts.RemoveRange(ctxContactType.Contacts);
}
ctx.ContactTypes.RemoveRange(ctx.ContactTypes);
ctx.SaveChanges();
*/
foreach (var person in ctx.People
.Include(a => a.Contacts)
.ThenInclude(b => b.ContactType)
.Where(a => a.FirstName.ToUpper().Contains("DRE"))
)
.ThenInclude(b => b.ContactType))
{
Console.WriteLine(person);
foreach (var personContact in person.Contacts)
{
Console.WriteLine(
"\t" +
personContact.ContactValue + " - " +
personContact.ContactType.ContactTypeValue);
"\t" +
personContact.ContactValue + " - " +
personContact.ContactType.ContactTypeValue +
$"({ personContact.ContactType.ContactTypeId})");
}
}
}
......
using System;
using System.Linq;
using Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
......@@ -33,6 +34,23 @@ namespace DAL
);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// configure entities
modelBuilder.Entity<ContactType>()
.HasIndex(i => i.ContactTypeValue).IsUnique();
// remove Cascade delete from all the entities <- this has to be at the end
foreach (var mutableForeignKey in
modelBuilder.Model.GetEntityTypes()
.Where(e => e.IsOwned() == false)
.SelectMany(e => e.GetForeignKeys()))
{
mutableForeignKey.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
}
}
// <auto-generated />
using DAL;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace DAL.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20181109072807_RemoveCascadeAddMaxLen")]
partial class RemoveCascadeAddMaxLen
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.4-rtm-31024")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Domain.Contact", b =>
{
b.Property<int>("ContactId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<int>("ContactTypeId");
b.Property<string>("ContactValue")
.HasMaxLength(128);
b.Property<int>("PersonId");
b.HasKey("ContactId");
b.HasIndex("ContactTypeId");
b.HasIndex("PersonId");
b.ToTable("Contacts");
});
modelBuilder.Entity("Domain.ContactType", b =>
{
b.Property<int>("ContactTypeId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("ContactTypeValue")
.HasMaxLength(64);
b.HasKey("ContactTypeId");
b.ToTable("ContactTypes");
});
modelBuilder.Entity("Domain.Person", b =>
{
b.Property<int>("PersonId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("FirstName")
.HasMaxLength(64);
b.Property<string>("LastName")
.HasMaxLength(64);
b.HasKey("PersonId");
b.ToTable("People");
});
modelBuilder.Entity("Domain.Contact", b =>
{
b.HasOne("Domain.ContactType", "ContactType")
.WithMany("Contacts")
.HasForeignKey("ContactTypeId")
.OnDelete(DeleteBehavior.Restrict);
b.HasOne("Domain.Person", "Person")
.WithMany("Contacts")
.HasForeignKey("PersonId")
.OnDelete(DeleteBehavior.Restrict);
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace DAL.Migrations
{
public partial class RemoveCascadeAddMaxLen : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Contacts_ContactTypes_ContactTypeId",
table: "Contacts");
migrationBuilder.DropForeignKey(
name: "FK_Contacts_People_PersonId",
table: "Contacts");
migrationBuilder.AlterColumn<string>(
name: "ContactTypeValue",
table: "ContactTypes",
maxLength: 64,
nullable: true,
oldClrType: typeof(string),
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "ContactValue",
table: "Contacts",
maxLength: 128,
nullable: true,
oldClrType: typeof(string),
oldNullable: true);
migrationBuilder.AddForeignKey(
name: "FK_Contacts_ContactTypes_ContactTypeId",
table: "Contacts",
column: "ContactTypeId",
principalTable: "ContactTypes",
principalColumn: "ContactTypeId",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Contacts_People_PersonId",
table: "Contacts",
column: "PersonId",
principalTable: "People",
principalColumn: "PersonId",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Contacts_ContactTypes_ContactTypeId",
table: "Contacts");
migrationBuilder.DropForeignKey(
name: "FK_Contacts_People_PersonId",
table: "Contacts");
migrationBuilder.AlterColumn<string>(
name: "ContactTypeValue",
table: "ContactTypes",
nullable: true,
oldClrType: typeof(string),
oldMaxLength: 64,
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "ContactValue",
table: "Contacts",
nullable: true,
oldClrType: typeof(string),
oldMaxLength: 128,
oldNullable: true);
migrationBuilder.AddForeignKey(
name: "FK_Contacts_ContactTypes_ContactTypeId",
table: "Contacts",
column: "ContactTypeId",
principalTable: "ContactTypes",
principalColumn: "ContactTypeId",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_Contacts_People_PersonId",
table: "Contacts",
column: "PersonId",
principalTable: "People",
principalColumn: "PersonId",
onDelete: ReferentialAction.Cascade);
}
}
}
// <auto-generated />
using DAL;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace DAL.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20181109074025_AddIndexOnContactTypeValue")]
partial class AddIndexOnContactTypeValue
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.4-rtm-31024")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Domain.Contact", b =>
{
b.Property<int>("ContactId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<int>("ContactTypeId");
b.Property<string>("ContactValue")
.HasMaxLength(128);
b.Property<int>("PersonId");
b.HasKey("ContactId");
b.HasIndex("ContactTypeId");
b.HasIndex("PersonId");
b.ToTable("Contacts");
});
modelBuilder.Entity("Domain.ContactType", b =>
{
b.Property<int>("ContactTypeId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("ContactTypeValue")
.HasMaxLength(64);
b.HasKey("ContactTypeId");
b.HasIndex("ContactTypeValue")
.IsUnique()
.HasFilter("[ContactTypeValue] IS NOT NULL");
b.ToTable("ContactTypes");
});
modelBuilder.Entity("Domain.Person", b =>
{
b.Property<int>("PersonId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("FirstName")
.HasMaxLength(64);
b.Property<string>("LastName")
.HasMaxLength(64);
b.HasKey("PersonId");
b.ToTable("People");
});
modelBuilder.Entity("Domain.Contact", b =>
{
b.HasOne("Domain.ContactType", "ContactType")
.WithMany("Contacts")
.HasForeignKey("ContactTypeId")
.OnDelete(DeleteBehavior.Restrict);
b.HasOne("Domain.Person", "Person")
.WithMany("Contacts")
.HasForeignKey("PersonId")
.OnDelete(DeleteBehavior.Restrict);
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace DAL.Migrations
{
public partial class AddIndexOnContactTypeValue : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_ContactTypes_ContactTypeValue",
table: "ContactTypes",
column: "ContactTypeValue",
unique: true,
filter: "[ContactTypeValue] IS NOT NULL");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_ContactTypes_ContactTypeValue",
table: "ContactTypes");
}
}
}
......@@ -26,7 +26,8 @@ namespace DAL.Migrations
b.Property<int>("ContactTypeId");
b.Property<string>("ContactValue");
b.Property<string>("ContactValue")
.HasMaxLength(128);
b.Property<int>("PersonId");
......@@ -45,10 +46,15 @@ namespace DAL.Migrations
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("ContactTypeValue");
b.Property<string>("ContactTypeValue")
.HasMaxLength(64);
b.HasKey("ContactTypeId");
b.HasIndex("ContactTypeValue")
.IsUnique()
.HasFilter("[ContactTypeValue] IS NOT NULL");
b.ToTable("ContactTypes");
});
......@@ -74,12 +80,12 @@ namespace DAL.Migrations
b.HasOne("Domain.ContactType", "ContactType")
.WithMany("Contacts")
.HasForeignKey("ContactTypeId")
.OnDelete(DeleteBehavior.Cascade);
.OnDelete(DeleteBehavior.Restrict);
b.HasOne("Domain.Person", "Person")
.WithMany("Contacts")
.HasForeignKey("PersonId")
.OnDelete(DeleteBehavior.Cascade);
.OnDelete(DeleteBehavior.Restrict);
});
#pragma warning restore 612, 618
}
......
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Domain
......@@ -8,6 +9,7 @@ namespace Domain
{
public int ContactId { get; set; }
[MaxLength(128)]
public string ContactValue { get; set; }
// Convention - FK for Person type
......
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Text;
......@@ -9,6 +10,7 @@ namespace Domain
{
public int ContactTypeId { get; set; }
[MaxLength(64)]
public string ContactTypeValue { get; set; }
public List<Contact> Contacts { get; set; } = new List<Contact>();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment