• 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

    公众号

  • 相关阅读:
    Android 应用程序集成FaceBook 登录及二次封装
    Android MVP 设计模式
    java 接口的作用和好处
    Android版本和API Level对应关系
    Android 开源库和项目 2
    高效开发iOS系列 -- 那些不为人知的KVC
    HDU 1019 Least Common Multiple 数学题解
    程序猿喜欢如何的职位描写叙述?
    从零開始搭建微信硬件开发环境全过程——1小时掌握微信硬件开发流程
    Spring ORM数据訪问——Hibernate
  • 原文地址:https://www.cnblogs.com/MrChuJiu/p/15838193.html
Copyright © 2020-2023  润新知