forked from yangzhongke/NETBookMaterials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6bde48
commit 3c4f479
Showing
41 changed files
with
1,505 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Delivery | ||
{ | ||
public long Id { get; set; } | ||
public string CompanyName { get; set; }//快递公司名 | ||
public String Number { get; set; }//快递单号 | ||
public Order Order { get; set; }//订单 | ||
public long OrderId { get; set; }//指向订单的外键 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
class DeliveryConfig : IEntityTypeConfiguration<Delivery> | ||
{ | ||
public void Configure(EntityTypeBuilder<Delivery> builder) | ||
{ | ||
builder.ToTable("T_Deliveries"); | ||
builder.Property(d => d.CompanyName).IsUnicode().HasMaxLength(10); | ||
builder.Property(d => d.Number).HasMaxLength(50); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace 一对一关系.Migrations | ||
{ | ||
public partial class init : Migration | ||
{ | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "T_Orders", | ||
columns: table => new | ||
{ | ||
Id = table.Column<long>(type: "bigint", nullable: false) | ||
.Annotation("SqlServer:Identity", "1, 1"), | ||
Name = table.Column<string>(type: "nvarchar(max)", nullable: false), | ||
Address = table.Column<string>(type: "nvarchar(max)", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_T_Orders", x => x.Id); | ||
}); | ||
|
||
migrationBuilder.CreateTable( | ||
name: "T_Deliveries", | ||
columns: table => new | ||
{ | ||
Id = table.Column<long>(type: "bigint", nullable: false) | ||
.Annotation("SqlServer:Identity", "1, 1"), | ||
CompanyName = table.Column<string>(type: "nvarchar(10)", maxLength: 10, nullable: false), | ||
Number = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false), | ||
OrderId = table.Column<long>(type: "bigint", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_T_Deliveries", x => x.Id); | ||
table.ForeignKey( | ||
name: "FK_T_Deliveries_T_Orders_OrderId", | ||
column: x => x.OrderId, | ||
principalTable: "T_Orders", | ||
principalColumn: "Id", | ||
onDelete: ReferentialAction.Cascade); | ||
}); | ||
|
||
migrationBuilder.CreateIndex( | ||
name: "IX_T_Deliveries_OrderId", | ||
table: "T_Deliveries", | ||
column: "OrderId", | ||
unique: true); | ||
} | ||
|
||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "T_Deliveries"); | ||
|
||
migrationBuilder.DropTable( | ||
name: "T_Orders"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// <auto-generated /> | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
#nullable disable | ||
|
||
namespace 一对一关系.Migrations | ||
{ | ||
[DbContext(typeof(TestDbContext))] | ||
partial class TestDbContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder | ||
.HasAnnotation("ProductVersion", "6.0.0") | ||
.HasAnnotation("Relational:MaxIdentifierLength", 128); | ||
|
||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); | ||
|
||
modelBuilder.Entity("Delivery", b => | ||
{ | ||
b.Property<long>("Id") | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("bigint"); | ||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"), 1L, 1); | ||
b.Property<string>("CompanyName") | ||
.IsRequired() | ||
.HasMaxLength(10) | ||
.IsUnicode(true) | ||
.HasColumnType("nvarchar(10)"); | ||
b.Property<string>("Number") | ||
.IsRequired() | ||
.HasMaxLength(50) | ||
.HasColumnType("nvarchar(50)"); | ||
b.Property<long>("OrderId") | ||
.HasColumnType("bigint"); | ||
b.HasKey("Id"); | ||
b.HasIndex("OrderId") | ||
.IsUnique(); | ||
b.ToTable("T_Deliveries", (string)null); | ||
}); | ||
|
||
modelBuilder.Entity("Order", b => | ||
{ | ||
b.Property<long>("Id") | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("bigint"); | ||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"), 1L, 1); | ||
b.Property<string>("Address") | ||
.IsRequired() | ||
.IsUnicode(true) | ||
.HasColumnType("nvarchar(max)"); | ||
b.Property<string>("Name") | ||
.IsRequired() | ||
.IsUnicode(true) | ||
.HasColumnType("nvarchar(max)"); | ||
b.HasKey("Id"); | ||
b.ToTable("T_Orders", (string)null); | ||
}); | ||
|
||
modelBuilder.Entity("Delivery", b => | ||
{ | ||
b.HasOne("Order", "Order") | ||
.WithOne("Delivery") | ||
.HasForeignKey("Delivery", "OrderId") | ||
.OnDelete(DeleteBehavior.Cascade) | ||
.IsRequired(); | ||
b.Navigation("Order"); | ||
}); | ||
|
||
modelBuilder.Entity("Order", b => | ||
{ | ||
b.Navigation("Delivery"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Order | ||
{ | ||
public long Id { get; set; } | ||
public string Name { get; set; }//商品名 | ||
public string Address { get; set; }//收货地址 | ||
public Delivery? Delivery { get; set; }//快递信息 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
class OrderConfig : IEntityTypeConfiguration<Order> | ||
{ | ||
public void Configure(EntityTypeBuilder<Order> builder) | ||
{ | ||
builder.ToTable("T_Orders"); | ||
builder.Property(o => o.Address).IsUnicode(); | ||
builder.Property(o => o.Name).IsUnicode(); | ||
builder.HasOne<Delivery>(o => o.Delivery).WithOne(d => d.Order) | ||
.HasForeignKey<Delivery>(d => d.OrderId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
using TestDbContext ctx = new TestDbContext(); | ||
Order order = new Order(); | ||
order.Address = "北京市海淀区中关村南大街999号"; | ||
order.Name = "USB充电器"; | ||
Delivery delivery = new Delivery(); | ||
delivery.CompanyName = "蜗牛快递"; | ||
delivery.Number = "SN333322888"; | ||
delivery.Order = order; | ||
ctx.Deliveries.Add(delivery); | ||
await ctx.SaveChangesAsync(); | ||
Order order1 = await ctx.Orders.Include(o => o.Delivery) | ||
.FirstAsync(o => o.Name.Contains("充电器")); | ||
Console.WriteLine($"名称:{order1.Name},单号:{order1.Delivery.Number}"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
class TestDbContext : DbContext | ||
{ | ||
public DbSet<Delivery> Deliveries { get; set; } | ||
public DbSet<Order> Orders { get; set; } | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
string connStr = "Server=.;Database=demo6;Trusted_Connection=True"; | ||
optionsBuilder.UseSqlServer(connStr); | ||
optionsBuilder.LogTo(Console.WriteLine); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Leave | ||
{ | ||
public long Id { get; set; } | ||
public User Requester { get; set; }//申请者 | ||
public User? Approver { get; set; } //审批者 | ||
public string Remarks { get; set; } //说明 | ||
public DateTime From { get; set; } //开始日期 | ||
public DateTime To { get; set; } //结束日期 | ||
public int Status { get; set; }//状态 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
class LeaveConfig : IEntityTypeConfiguration<Leave> | ||
{ | ||
public void Configure(EntityTypeBuilder<Leave> builder) | ||
{ | ||
builder.ToTable("T_Leaves"); | ||
builder.HasOne<User>(l => l.Requester).WithMany(); | ||
builder.HasOne<User>(l => l.Approver).WithMany(); | ||
builder.Property(l => l.Remarks).HasMaxLength(1000).IsUnicode(); | ||
} | ||
} |
Oops, something went wrong.