如何向 EF Core SqlServer 目标生成器添加对结构化注释的支持? [英] How to add the support for structured annotations to the EF Core SqlServer target builder?

本文介绍了如何向 EF Core SqlServer 目标生成器添加对结构化注释的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用接受 object 类型参数的 HasAnnotation 方法向 EF 模型添加纯字符串注释很容易,但是尝试添加结构化注释时会出错构建迁移:

It is easy to add plain string annotations to the EF model with HasAnnotation method that accept object type of argument, but trying to add structured annotations you will get errors on build migration:

        modelBuilder.Entity<Group>().HasAnnotation($"{GetType().FullName}.Constraint3", new ValueTuple<string,string>( "aaa", "bbb" ));

The current CSharpHelper cannot scaffold literals of type 'System.ValueTuple`2[System.String,System.String]'. Configure your services to use one that can.

        modelBuilder.Entity<Group>().HasAnnotation($"{GetType().FullName}.Constraint2", new[] { "aaa", "bbb" });

The current CSharpHelper cannot scaffold literals of type 'System.String[]'. Configure your services to use one that can.

我们是否有办法向 target builder 注入有助于序列化结构化注解的方法?

Do we have a way to inject a method to the target builder that would help to serialize structured annotations?

我为什么需要它?只是试图在一个地方收集所有数据库元.而元数据通常是结构化信息.

Why I would need it? Just trying to collect all database meta in one place. And meta is usually structured info.

推荐答案

据我所知(关于这方面的信息不多,如果有的话),想法是使用简单的名称/值对,其中值是原始类型(stringintdecimalDateTime 等加上 enums) 和名称构成结构.所有 EF Core 注释都遵循该原则.例如,名称:"SqlServer:ValueGenerationStrategy" 类型:SqlServerValueGenerationStrategy

As far as I understand (there is not much if any at all information about that), the idea is to use simple name / value pairs, where the values are primitive types (string, int, decimal, DateTime etc. plus enums) and names form the structure. All EF Core annotations follow that principle. For instance, Name: "SqlServer:ValueGenerationStrategy" Type: SqlServerValueGenerationStrategy etc.

可能最简单的方法就是简单地遵循该约定.但无论如何,负责的服务是 ICSharpHelper - UnknownLiteral 方法.默认实现位于 CSharpHelper 类.

Probably the easiest is to simply follow that convention. But anyway, the responsible service is ICSharpHelper - UnknownLiteral method. And the default implementation is in CSharpHelper class.

所以你可以从中派生,覆盖 UnknownLiteral 方法并在那里做你自己的(预)处理:

So you can derive from it, override the UnknownLiteral method and do your own (pre)processing there:

using Microsoft.EntityFrameworkCore.Design.Internal;

public class MyCSharpHelper : CSharpHelper
{
    public override string UnknownLiteral(object value)
    {
        // Preprocess the value here...
        return base.UnknownLiteral(value);
    }
}

然后用您的服务替换标准服务:

then replace the standard service with yours:

using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.DependencyInjection;

public class MyDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection services)
    {
        services.AddSingleton<ICSharpHelper, MyCSharpHelper>();
    }
}

这篇关于如何向 EF Core SqlServer 目标生成器添加对结构化注释的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆