我确定之前有人问过这个问题,我不知道要搜索什么,所以它可能是重复的。我有将新实体添加到数据库的代码。该实体引用了另一个实体(Role),我通过服务获取它。服务创建了另一个实例dbContext,因此我必须在获取角色后将其附加到上下文。问题是,当我尝试附加两个相同的角色时,出现以下异常:
无法跟踪“角色”,因为已跟踪具有相同 {'Id'} 键值的另一个实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来查看冲突的键值。
我该怎么做呢?代码如下:
using (var context = new TenantContext(schemaName, connectionString))
{
ApprovalTemplates templates = new ApprovalTemplates();
ApprovalTemplate template = new ApprovalTemplate();
template.Approvers = new List<StageTemplate>();
foreach (var stage in request.Stages)
{
var temp = new StageTemplate();
temp.Order = stage.Order;
temp.Name = stage.Name;
var role = roleService.GetById(stage.RoleId, schemaName);//here I get the role
temp.AvailableActions = new List<ApprovalActionTemplate>();
foreach (var actionId in stage.Actions)
temp.AvailableActions.Add(context.ApprovalActions.First(a => a.Id == actionId));
//when I try to add already attached role, exception …Run Code Online (Sandbox Code Playgroud) 我正在将Microsoft.AspNetCore.Identity添加到项目中,我得到了
InvalidOperationException:尝试激活'Web.Security.Services.SecurityService'2时无法解析类型'Microsoft.AspNetCore.Identity.SignInManager'1 [Web.Security.Entities.IUser'1 [System.Int32]]'的服务Web.Security.Entities.IUser'1 [System.Int32],System.Int32]".
例外是邮递员的复制品,它编码了一些符号.这是我的Startup.cs:
public class Startup
{
ServiceProvider serviceProvider;
IConfigurationRoot configurationRoot;
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
configurationRoot = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.AddIniFile("3CXPhoneSystem.ini")
.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
serviceProvider = services
.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace))
.AddSingleton<ISecurityService, SecurityService<IUser<int>, int>>() …Run Code Online (Sandbox Code Playgroud) 我发现了许多类似的问题,并发布了如何进行的方法,但是我不确定哪种方法更好。我认为我需要一些DbContextFactory类,该类将根据TenantId返回上下文,但是我不知道如何使用OnModelCreating实现此目的。我主要看到有关每租户数据库体系结构的文章,但不确定是否知道如何将架构绑定到上下文(通过用户?)。我试图遵循此 https://romiller.com/2011/05/23/ef-4-1-multi-tenant-with-code-first/, 但看起来这不适用于最新的EF版本。我还使用代码优先 EF6 检查了此 多租户,但IDbModelCacheKeyProvider更改了,现在在Create中需要DbContext,这与我想要做的相反。能否请您举一个例子说明如何完成?
我想添加自定义策略以进行两步授权,因此在第一步之后,用户将可以访问第二步,然后再访问所有内容。当我有一项政策时,一切正常,但当我添加另一项政策时,我开始收到此错误。我如何添加策略:
AuthorizationOptions authOptions = new AuthorizationOptions();
authOptions.AddPolicy("FirstStepCompleted", policy => policy.RequireClaim("FirstStepCompleted"));
authOptions.AddPolicy("Authorized", policy => policy.RequireClaim("Authorized"));
services.AddAuthorization(o => o = authOptions);
Run Code Online (Sandbox Code Playgroud)
当我有一项政策时,我像这样添加它:
services.AddAuthorization
(
options => options.AddPolicy("FirstStepCompleted",
policy => policy.RequireClaim("FirstStepCompleted"))
);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
UPD1。忘记了例外:
处理请求时发生未处理的异常。InvalidOperationException:未找到名为“FirstStepCompleted”的 AuthorizationPolicy。Microsoft.AspNetCore.Authorization.AuthorizationPolicy.CombineAsync(IAuthorizationPolicyProviderpolicyProvider,IEnumerableauthorizeData)
我有一个 WebAPI,它也应该从 RabbitMQ 接收消息。我使用了这个教程,因为我知道有时 IIS 喜欢终止长时间运行的任务(虽然还没有在服务器上测试它,也许它不起作用)。我有一个处理通过 RabbitMQ 接收的消息的服务。我遇到的第一个问题 - 我无法将它注入到BackgroundService类中,所以我使用了IServiceScopeFactory. 现在,我必须使用来自两个队列的消息,据我了解,最佳实践是为此使用两个通道。但处理是在一项服务中完成的。后台服务:
public class ConsumeRabbitMQHostedService : BackgroundService
{
private IConnection _connection;
private IModel _firstChannel;
private IModel _secondChannel;
private RabbitConfigSection _rabbitConfig;
public IServiceScopeFactory _serviceScopeFactory;
public ConsumeRabbitMQHostedService(IOptions<RabbitConfigSection> rabbitConfig, IServiceScopeFactory serviceScopeFactory)
{
_rabbitConfig = rabbitConfig.Value;
_serviceScopeFactory = serviceScopeFactory;
InitRabbitMQ();
}
private void InitRabbitMQ()
{
var factory = new ConnectionFactory { HostName = _rabbitConfig.HostName, UserName = _rabbitConfig.UserName, Password = _rabbitConfig.Password };
_connection = factory.CreateConnection();
_firstChannel = _connection.CreateModel();
_firstChannel.ExchangeDeclare(_rabbitConfig.DefaultExchange, ExchangeType.Topic); …Run Code Online (Sandbox Code Playgroud) c# rabbitmq entity-framework-core asp.net-core asp.net-core-hosted-services
我发现了类似的问题,但没有一个对我有帮助。我需要将表格标题中的背景颜色设置为白色,现在为灰色(与表格的背景颜色相同)。
这是我的tableView功能:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header = view as! UITableViewHeaderFooterView
header.backgroundColor = UIColor.white
header.textLabel?.font = UIFont(name: "Futura", size: 13)!
header.textLabel?.textColor = UIColor.blue
}
func tableView(tableView: UITableView, ViewForHeaderInSection section: Int) -> UIView? {
tableView.backgroundColor = UIColor.white
return tableView
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return titles[section]
}
Run Code Online (Sandbox Code Playgroud)
我将背景色设置为白色,但是没有任何变化。
我正在使用带有.net核心的mvc,我需要在启动时运行一个任务,并在应用程序停止时停止它.在Startup.cs中,我注册了应用程序启动和停止的事件.问题是,我不知道如何运行必须在启动时在特定类中运行的任务.任务看起来像这样:
public void PreventStatusChange()
{
while (forceStatusChange)
{
foreach (var ext in GetExtensions())
{
ext.Status = StatusType.Available;
}
Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
变量forceStatusChange在同一个类中声明,所以我没有从我的Startup.cs中看到它.最好的方法是什么?
c# ×6
asp.net-core ×5
asp.net-core-hosted-services ×1
asp.net-mvc ×1
rabbitmq ×1
swift ×1
uitableview ×1