Skip to content

Commit

Permalink
用洋葱架构重构
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhongke committed Oct 29, 2021
1 parent b5e30b7 commit dcb92f6
Show file tree
Hide file tree
Showing 27 changed files with 588 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFcoreOneToOneBug
namespace EFcoreOneToOneBug
{
class A
{
public Guid Id { get; set; }
public string Name { get; set; }
public B? B { get; set; }
public Guid? BId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFcoreOneToOneBug
namespace EFcoreOneToOneBug
{
class B
{
public Guid Id { get; set; }
public int Age { get; set; }
public A A { get; set; }
public Guid AId { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace EFcoreOneToOneBug.Migrations
{
public partial class Init : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "A",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_A", x => x.Id);
});

migrationBuilder.CreateTable(
name: "B",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Age = table.Column<int>(type: "int", nullable: false),
AId = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_B", x => x.Id);
table.ForeignKey(
name: "FK_B_A_AId",
column: x => x.AId,
principalTable: "A",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_B_AId",
table: "B",
column: "AId",
unique: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "B");

migrationBuilder.DropTable(
name: "A");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace EFcoreOneToOneBug.Migrations
{
public partial class Init2 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "BId",
table: "A",
type: "uniqueidentifier",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "BId",
table: "A");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// <auto-generated />
using System;
using EFcoreOneToOneBug;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace EFcoreOneToOneBug.Migrations
{
[DbContext(typeof(MyDbContext))]
partial class MyDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0-rc.2.21480.5")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

modelBuilder.Entity("EFcoreOneToOneBug.A", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<Guid?>("BId")
.HasColumnType("uniqueidentifier");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("A");
});

modelBuilder.Entity("EFcoreOneToOneBug.B", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<Guid>("AId")
.HasColumnType("uniqueidentifier");
b.Property<int>("Age")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("AId")
.IsUnique();
b.ToTable("B");
});

modelBuilder.Entity("EFcoreOneToOneBug.B", b =>
{
b.HasOne("EFcoreOneToOneBug.A", "A")
.WithOne("B")
.HasForeignKey("EFcoreOneToOneBug.B", "AId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("A");
});

modelBuilder.Entity("EFcoreOneToOneBug.A", b =>
{
b.Navigation("B");
});
#pragma warning restore 612, 618
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ class MyDbContext:DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.LogTo(Console.WriteLine);
optionsBuilder.UseSqlServer("Server=.;Database=testbug;Trusted_Connection=True;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<A>().HasOne(x => x.B).WithOne(x=>x.A);
modelBuilder.Entity<A>().HasOne(x => x.B).WithOne(x => x.A)
//.IsRequired(false)
.HasForeignKey<B>(x => x.AId);//.HasPrincipalKey<A>(x=>x.BId);
}
}
}
Loading

0 comments on commit dcb92f6

Please sign in to comment.