.net 如何向EF Core SqlServer目标生成器添加对结构化注释的支持?

.net 如何向EF Core SqlServer目标生成器添加对结构化注释的支持?,.net,entity-framework,annotations,entity-framework-core,entity-framework-core-migrations,.net,Entity Framework,Annotations,Entity Framework Core,Entity Framework Core Migrations,使用接受object参数类型的hasaannotation方法,可以很容易地向EF模型添加纯字符串注释,但尝试添加结构化注释会在生成迁移时出错: modelBuilder.Entity<Group>().HasAnnotation($"{GetType().FullName}.Constraint3", new ValueTuple<string,string>( "aaa", "bbb" )); The current CSharpHelper can

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

        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.
modelBuilder.Entity().HasAnnotation($“{GetType().FullName}.Constraint3”,新的ValueTuple(“aaa”,“bbb”);
当前CSharpHelper无法为类型为“System.ValueTuple`2[System.String,System.String]”的文本搭建脚手架。将您的服务配置为使用一个可以。
modelBuilder.Entity().HasAnnotation($“{GetType().FullName}.Constraint2”,新[]{“aaa”,“bbb”});
当前CSharpHelper无法构造“System.String[]”类型的文本。将您的服务配置为使用一个可以。
我们是否有办法向
目标构建器
注入一种方法,以帮助序列化结构化注释

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

据我所知(关于这方面的信息几乎没有),其思想是使用简单的名称/值对,其中值是基本类型(
string
int
decimal
DateTime
等,加上
enum
s)名字构成了结构。所有EF核心注释都遵循这一原则。例如,名称:
“SqlServer:ValueGenerationsStrategy”
类型:
SQLServerValueGenerationsStrategy

也许最简单的方法就是简单地遵循这个惯例。但无论如何,负责任的服务是-方法。默认实现在类中

因此,您可以从中派生,重写
UnknownLiteral
方法,并在那里进行自己的(预)处理:

using Microsoft.EntityFrameworkCore.Design.Internal;

public class MyCSharpHelper : CSharpHelper
{
    public override string UnknownLiteral(object value)
    {
        // Preprocess the value here...
        return base.UnknownLiteral(value);
    }
}
然后用您的服务替换标准服务:

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

public class MyDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection services)
    {
        services.AddSingleton<ICSharpHelper, MyCSharpHelper>();
    }
}
使用Microsoft.EntityFrameworkCore.Design;
使用Microsoft.Extensions.DependencyInjection;
公共类MyDesignTimeServices:IDesignTimeServices
{
public void配置DesignTimeServices(iSeries收集服务)
{
services.AddSingleton();
}
}

当我撰写问题时,我尝试了POCO,结果相同(错误)。。。但是现在我会用它做更多的实验。嗯,我在发布之前就试过了,而且效果很好。对于第一个示例,代码类似于
if(value是ValueTuple vt)return$“newvaluetuple({Literal(vt.Item1)},{Literal(vt.Item2)}”并在迁移
designer.cs
文件中生成正确的
hasnotation
。通过临时放置
抛出新异常(“Blah”)来确保调用您的服务位于重写方法的开头。使用EFC2.0.1进行测试,如果这很重要的话。是的,这是一种非常有效的方式(直接将文本插入到迁移源中)。。。我对“液体密码向导”有一种很好的感觉。非常感谢你。至少ef core中的迁移是以灵活的方式进行的(我对ef core日志记录和ef core DI“仅容器”的方式非常不满意)。Ivan,也许我可以请你谈谈你对这些问题的看法(首先是继续尝试重用ef core元模型):1)2)Hi@Roman,当然,我会在有时间的时候告诉你。但是请注意,我没有参与EF核心开发,所以我所有的帖子都是基于文档、实验和探索源代码:)