Skip to content

Commit

Permalink
代码完成到4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhongke committed Nov 10, 2021
1 parent c0fc9f4 commit d6bde48
Show file tree
Hide file tree
Showing 27 changed files with 788 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 第四章/EFCore实体配置DataAnnotations/Author.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Author
{
public Guid Id { get; set; }
public string Name { get; set; }
}
16 changes: 16 additions & 0 deletions 第四章/EFCore实体配置DataAnnotations/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("T_Books")]
public class Book
{
public long Id { get; set; }//主键
[MaxLength(50)]
[Required]
public string Title { get; set; }//标题
public DateTime PubTime { get; set; }//发布日期
public double Price { get; set; }//单价
[MaxLength(20)]
[Required]
public string AuthorName { get; set; }//作者名字
}
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>

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,50 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace EFCore实体配置DataAnnotations.Migrations
{
public partial class AddAuthor : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Authors",
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_Authors", x => x.Id);
});

migrationBuilder.CreateTable(
name: "T_Books",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
PubTime = table.Column<DateTime>(type: "datetime2", nullable: false),
Price = table.Column<double>(type: "float", nullable: false),
AuthorName = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_T_Books", x => x.Id);
});
}

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

migrationBuilder.DropTable(
name: "T_Books");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace EFCore实体配置DataAnnotations.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("Author", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Authors");
});

modelBuilder.Entity("Book", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"), 1L, 1);
b.Property<string>("AuthorName")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("nvarchar(20)");
b.Property<double>("Price")
.HasColumnType("float");
b.Property<DateTime>("PubTime")
.HasColumnType("datetime2");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.HasKey("Id");
b.ToTable("T_Books");
});
#pragma warning restore 612, 618
}
}
}
30 changes: 30 additions & 0 deletions 第四章/EFCore实体配置DataAnnotations/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
using TestDbContext ctx = new TestDbContext();
Book b = new Book { AuthorName = "Bill Gates", Title = "Zack, Cool guy!",
Price = 9.9, PubTime = new DateTime(2020, 12, 30) };
ctx.Books.Add(b);
Console.WriteLine($"保存前,Id={b.Id}");
await ctx.SaveChangesAsync();
Console.WriteLine($"保存后,Id={b.Id}");*/
/*
using TestDbContext ctx = new TestDbContext() ;
Console.WriteLine("****1*****");
Author a1 = new Author { Name = "杨中科" };
Console.WriteLine($"Add前,Id={a1.Id}");
ctx.Authors.Add(a1);
Console.WriteLine($"Add后,保存前,Id={a1.Id}");
await ctx.SaveChangesAsync();
Console.WriteLine($"保存后,Id={a1.Id}");
Console.WriteLine("****2*****");
Author a2 = new Author { Name = "Zack Yang" };
a2.Id = Guid.NewGuid();
Console.WriteLine($"保存前,Id={a2.Id}");
ctx.Authors.Add(a2);
await ctx.SaveChangesAsync();
Console.WriteLine($"保存前,Id={a2.Id}");*/
using TestDbContext ctx = new TestDbContext();
var books = ctx.Books.Where(b => b.PubTime.Year > 2010).Take(3);
foreach (var b in books)
{
Console.WriteLine(b.Title);
}
18 changes: 18 additions & 0 deletions 第四章/EFCore实体配置DataAnnotations/TestDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
class TestDbContext : DbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connStr = "Server=.;Database=demo2;Trusted_Connection=True";
optionsBuilder.UseSqlServer(connStr);
optionsBuilder.LogTo(Console.WriteLine);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
}
16 changes: 16 additions & 0 deletions 第四章/MySQL版项目/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("T_Books")]
public class Book
{
public long Id { get; set; }//主键
[MaxLength(50)]
[Required]
public string Title { get; set; }//标题
public DateTime PubTime { get; set; }//发布日期
public double Price { get; set; }//单价
[MaxLength(20)]
[Required]
public string AuthorName { get; set; }//作者名字
}
16 changes: 16 additions & 0 deletions 第四章/MySQL版项目/MySQL版项目.csproj
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="Pomelo.EntityFrameworkCore.MySql" 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>
6 changes: 6 additions & 0 deletions 第四章/MySQL版项目/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using TestDbContext ctx = new TestDbContext();
var books = ctx.Books.Where(b => b.PubTime.Year > 2010).Take(3);
foreach (var b in books)
{
Console.WriteLine(b.Title);
}
17 changes: 17 additions & 0 deletions 第四章/MySQL版项目/TestDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
class TestDbContext : DbContext
{
public DbSet<Book> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql("server=localhost;user=root;password=adfa3_ioz09_08nljo;database=ef",
new MySqlServerVersion(new Version(8, 6, 20)));
optionsBuilder.LogTo(Console.WriteLine);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
}
16 changes: 16 additions & 0 deletions 第四章/PostgreSQL版项目/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("T_Books")]
public class Book
{
public long Id { get; set; }//主键
[MaxLength(50)]
[Required]
public string Title { get; set; }//标题
public DateTime PubTime { get; set; }//发布日期
public double Price { get; set; }//单价
[MaxLength(20)]
[Required]
public string AuthorName { get; set; }//作者名字
}
16 changes: 16 additions & 0 deletions 第四章/PostgreSQL版项目/PostgreSQL版项目.csproj
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="Npgsql.EntityFrameworkCore.PostgreSQL" 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>
6 changes: 6 additions & 0 deletions 第四章/PostgreSQL版项目/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using TestDbContext ctx = new TestDbContext();
var books = ctx.Books.Where(b => b.PubTime.Year > 2010).Take(3);
foreach (var b in books)
{
Console.WriteLine(b.Title);
}
Loading

0 comments on commit d6bde48

Please sign in to comment.