如何将对结构化注释的支持添加到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等,加上enum),并且名称构成结构.所有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 方法.默认实现在

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天全站免登陆