介绍
接下来我将给大家重点介绍一下.Net 6 之后的一些新的变更,文章都是来自于外国大佬的文章,我这边进行一个翻译,并加上一些自己的理解和解释。
源作者链接:https://blog.okyrylchuk.dev/entity-framework-core-6-features-part-2
正文
HasConversion 支持值转换器
在 EF Core 中,HasConversion方法的泛型重载使用泛型参数来指定要转换为的类型。在 EF Core 6.0 中,泛型类型还可以指定内置或自定义值转换器。
public class ExampleContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Person>()
.Property(p => p.Address)
.HasConversion<AddressConverter>();
}
}
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Country { get; set; }
public string Street { get; set; }
public string ZipCode { get; set; }
}
public class AddressConverter : ValueConverter<Address, string>
{
public AddressConverter()
: base(
v => JsonSerializer.Serialize(v, (JsonSerializerOptions)null),
v => JsonSerializer.Deserialize<Address>(v, (JsonSerializerOptions)null))
{
}
}
多对多,无需额外配置
从 EF Core 6.0 开始,您可以使用任何其他配置以多对多关系配置联接实体。此外,您可以配置连接实体,而无需明确指定左右关系。
public class BloggingContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<PostTag> PostTags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasMany(p => p.Tags)
.WithMany(t => t.Posts)
.UsingEntity<PostTag>();
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=EFCore6Many2Many;Trusted_Connection=True;");
}
public class Post
{
public int Id { get; set; }
public string Name { get; set; }
public List<Tag> Tags { get; set; } = new List<Tag>();
}
public class Tag
{
public int Id { get; set; }
public string Text { get; set; }
public List<Post> Posts { get; set; } = new List<Post>();
}
public class PostTag
{
public int PostId { get; set; }
public int TagId { get; set; }
public DateTime AddedDate { get; set; }
}
脚手架多对多改进
EF Core 6.0 改进了现有数据库的脚手架。它检测连接表并为它们生成多对多映射。对于连接表,配置将被脚手架,没有类。
有示例数据库:
使用包管理器控制台的脚手架:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=EFCore6Many2Many;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Context ExampleContext -OutputDir Models
使用 CLI 的脚手架:
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=EFCore6Many2Many" Microsoft.EntityFrameworkCore.SqlServer --context ExampleContext --output-dir Models
OnModelCreating从生成的DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>(entity =>
{
entity.HasMany(d => d.Tags)
.WithMany(p => p.Posts)
.UsingEntity<Dictionary<string, object>>(
"PostTag",
l => l.HasOne<Tag>().WithMany().HasForeignKey("TagId"),
r => r.HasOne<Post>().WithMany().HasForeignKey("PostId"),
j =>
{
j.HasKey("PostId", "TagId")
j.ToTable("PostTags")
j.HasIndex(new[] { "TagId" }, "IX_PostTags_TagId");
});
});
OnModelCreatingPartial(modelBuilder);
}
结语
联系作者:加群:867095512 @MrChuJiu