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
c0fc9f4
commit d6bde48
Showing
27 changed files
with
788 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,5 @@ | ||
class Author | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { 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,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
16
第四章/EFCore实体配置DataAnnotations/EFCore实体配置DataAnnotations.csproj
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> |
72 changes: 72 additions & 0 deletions
72
第四章/EFCore实体配置DataAnnotations/Migrations/20211110021152_AddAuthor.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
第四章/EFCore实体配置DataAnnotations/Migrations/20211110021152_AddAuthor.cs
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,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"); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
第四章/EFCore实体配置DataAnnotations/Migrations/TestDbContextModelSnapshot.cs
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,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 | ||
} | ||
} | ||
} |
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,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); | ||
} |
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,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); | ||
} | ||
} |
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 @@ | ||
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; }//作者名字 | ||
} |
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="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> |
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,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); | ||
} |
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,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); | ||
} | ||
} |
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 @@ | ||
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; }//作者名字 | ||
} |
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="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> |
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,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); | ||
} |
Oops, something went wrong.