EF 6 新特性五

介绍

接下来我将给大家重点介绍一下.Net 6 之后的一些新的变更,文章都是来自于外国大佬的文章,我这边进行一个翻译,并加上一些自己的理解和解释。

源作者链接:https://blog.okyrylchuk.dev/entity-framework-core-6-features-part-2

正文

脚手架可为空的引用类型

EF Core 6.0 改进了现有数据库的脚手架。在项目中启用可空引用类型 (NRT) 时,EF Core 会自动使用 NRT 构建 DbContext 和实体类型。

有示例表:

CREATE TABLE [Posts] (
    [Id] int NOT NULL IDENTITY,
    [Name] nvarchar(max) NOT NULL,
    [Description] nvarchar(max) NULL,
    CONSTRAINT [PK_Posts] PRIMARY KEY ([Id])
)

生成的模型:

    public partial class Post
    {
        public int Id { get; set; }
        public string Name { get; set; } = null!;
        public string? Desciption { get; set; }
    }

脚手架数据库评论

EF Core 6.0 将数据库注释构建为代码注释。

有示例数据库:

CREATE TABLE [Posts] (
    [Id] int NOT NULL IDENTITY,
    [Name] nvarchar(max) NOT NULL,
    [Description] nvarchar(max) NULL,
    CONSTRAINT [PK_Posts] PRIMARY KEY ([Id]));
EXEC sp_addextendedproperty 
    @name = N'MS_Description', @value = 'The post table',
    @level0type = N'Schema', @level0name = dbo, 
    @level1type = N'Table',  @level1name = Posts
EXEC sp_addextendedproperty 
    @name = N'MS_Description', @value = 'The post identifier',
    @level0type = N'Schema', @level0name = dbo, 
    @level1type = N'Table',  @level1name = Posts, 
    @level2type = N'Column', @level2name = [Id];
EXEC sp_addextendedproperty 
    @name = N'MS_Description', @value = 'The post name',
    @level0type = N'Schema', @level0name = dbo, 
    @level1type = N'Table',  @level1name = Posts, 
    @level2type = N'Column', @level2name = [Name];
EXEC sp_addextendedproperty 
    @name = N'MS_Description', @value = 'The description name',
    @level0type = N'Schema', @level0name = dbo, 
    @level1type = N'Table',  @level1name = Posts, 
    @level2type = N'Column', @level2name = [Description];

生成的模型:

/// <summary>
/// The post table
/// </summary>
public partial class Post
{
    /// <summary>
    /// The post identifier
    /// </summary>
    public int Id { get; set; }
    /// <summary>
    /// The post name
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// The description name
    /// </summary>
    public string Description { get; set; }
}

AddDbContextFactory 注册 DbContext

在 EF Core 5.0 中,您可以注册一个工厂以手动创建DbContext实例。从 EF Core 6.0 开始, AddDbContextFactory 还注册DbContext。因此,您可以根据需要同时注入工厂和DbContext 。

var serviceProvider = new ServiceCollection()
        .AddDbContextFactory<ExampleContext>(builder => 
            builder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database = EFCore6Playground")) 
        .BuildServiceProvider();

var factory = serviceProvider.GetService<IDbContextFactory<ExampleContext>>();
using (var context = factory.CreateDbContext())
{
    // Contexts obtained from the factory must be explicitly disposed
}

using (var scope = serviceProvider.CreateScope())
{
    var context = scope.ServiceProvider.GetService<ExampleContext>();
    // Context is disposed when the scope is disposed
}
class ExampleContext : DbContext
{ }

结语

联系作者:加群:867095512 @MrChuJiu

EF 6 新特性五

上一篇:机器学习和深度学习知识路线


下一篇:vue重定向父路径无法向子路径传递参数