我有一个asp.net核心项目,它需要能够在运行时支持插件,因此,我需要根据已插入的内容生成数据库表.插件分为不同的项目,他们有他们自己的DbContext类.在编译期间,仅在运行时才知道要使用的插件.
现在在EF Core我认为会有像"UpdateDatabase"这样的方法,你可以在这里添加表到现有的数据库,但我错了.有没有办法实现这个目标?我能够为每个插件生成一个单独的数据库,但这并不是我想到的.我需要一个数据库中的所有表.
这是"HRContext"插件的代码:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Plugins.HR.Models.Entities;
namespace Plugins.HR.Contexts
{
public class HrContext : DbContext
{
public HrContext()
{
}
public HrContext(DbContextOptions<HrContext> contextOptions) : base(contextOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("HR");
base.OnModelCreating(modelBuilder);
}
public DbSet<Address> Address { get; set; }
public DbSet<Attendance> Attendance { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<JobTitle> JobTitles { get; set; }
}
} …Run Code Online (Sandbox Code Playgroud) 在不使用 ASP.NET Core 的 DI 的情况下,您可以使用其构造函数将包含 cookie 容器的 ClientHandler 放入 HttpClient 对象中,类似于以下内容:
var cookieContainer = new CookieContainer();
var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
var client = new HttpClient(handler)
{
BaseAddress = new Uri(uri),
Timeout = TimeSpan.FromMinutes(5)
};
Run Code Online (Sandbox Code Playgroud)
但是当您尝试使用 DI 时,您没有机会调用 HttpClient 的构造函数并将处理程序传入。我尝试了下一行代码:
services.AddHttpClient<HttpClient>(client =>
{
client.BaseAddress = new Uri(uri);
client.Timeout = TimeSpan.FromSeconds(5);
});
Run Code Online (Sandbox Code Playgroud)
但确实,当您获得实例时,它没有 cookie 容器。
我正在尝试 C 中的回调函数,但我不确定为什么,但出于某种原因 printf 在回调函数中不起作用。例如:
#include <stdio.h>
void call_this_method()
{
printf("call_this_method called\n");
}
void callback(void (*method))
{
printf("callback called\n");
(void) method;
}
int main(int argc, const char * argv[])
{
callback(call_this_method);
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行它,只有“回调调用”会在控制台中打印出来;“call_this_method called”不会被打印出来。这是为什么?