小编Nik*_*ruk的帖子

Asp.Net Core:将数据添加到IdentityDbContext或使用DbContext

我使用Asp.Net Core WebApi项目。

我可以像这样将表添加到IdentityDbContext

public class ApplicationDbContext : IdentityDbContext<User>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        { }

        public DbSet<ProgrammerRole> ProgrammerRoles { get; set; }

        public DbSet<Project> Projects { get; set; }
        public DbSet<SubProject> SubProjects { get; set; }

        public DbSet<Report> Reports { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

还是我需要创建第二个DbContext。如果我创建第二个DbContext,该如何与IdentityDbContext中的 User进行通信。

我的第二个问题:如果我像上面那样在IdentityDbContext中添加数据,如何从ApplicationDbContext中的表中获取数据?因为每次创建新实例时都需要传递DbContextOptions对象-f ApplicationDbContext。我在Startup.cs中这样做:

// ===== Add DbContext ========
            var connectionString = Configuration.GetConnectionString("DbConnection");
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(connectionString));
Run Code Online (Sandbox Code Playgroud)

我在较旧版本的Asp.Net Core中看到,我可以在IdentityDbContext构造函数中传递连接字符串,但现在只能传递DbContextOptions。而且我做不到,例如:

[AllowAnonymous]
        [HttpGet] …
Run Code Online (Sandbox Code Playgroud)

asp.net entity-framework-6 asp.net-identity entity-framework-core asp.net-core

3
推荐指数
1
解决办法
5054
查看次数

MvvmCross:Android布局绑定字符串资源

可能吗?:

//...
local:MvxBind="Text Format('{0} {1}', Stock, @string/in_stock)"/>
//...
Run Code Online (Sandbox Code Playgroud)

我想使用ViewModel的属性和strings.xml的字符串资源来构造文本值,但是上面的示例不起作用。

mvvmcross

2
推荐指数
1
解决办法
267
查看次数

Asp.Net Core:数据库中已经有一个名为“AspNetRoles”的对象

我创建了一个新的 Asp.Net Core WebApi 项目并简单地添加了 IdentityDbContext,然后我在Nuget Package Console 中编写了以下命令:

PM> Add-Migration Initial
To undo this action, use Remove-Migration.
PM> Update-Database
Applying migration '20180516150423_Initial'.
Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AspNetRoles] (
    [Id] nvarchar(450) NOT NULL,
    [ConcurrencyStamp] nvarchar(max) NULL,
    [Name] nvarchar(256) NULL,
    [NormalizedName] nvarchar(256) NULL,
    CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [AspNetRoles] (
          [Id] nvarchar(450) NOT NULL,
          [ConcurrencyStamp] nvarchar(max) NULL,
          [Name] nvarchar(256) NULL,
          [NormalizedName] nvarchar(256) …
Run Code Online (Sandbox Code Playgroud)

database asp.net entity-framework asp.net-core

1
推荐指数
1
解决办法
3578
查看次数

Asp.Net Core WebApi:授权属性错误403

我在Asp.Net Core WebApi项目中工作,并创建了一个角色“ admin”并将其添加到我的用户中。但是,如果我以管理员身份登录,第一种方法将返回“ This is admin!”。,但是第二种方法返回错误403 Forbidden。

如果我从Authorize属性中删除Roles参数,一切都会很好。我不明白为什么我无法访问第二种方法,因为我的用户具有管理员角色。

// Host/api/roles/getroles
        [Authorize]
        [HttpGet]
        public async Task<IEnumerable<string>> GetRoles()
        {
            var user = await _userManager.GetUserAsync(User);

            bool isAdmin = await _userManager.IsInRoleAsync(user, Roles.AdminRole);               
            if (isAdmin)
                return new[] {"This is admin!"};

            return await _userManager.GetRolesAsync(user);
        }


        // ===== Admin Methods =====

        // Host/api/roles/createrole
        [Authorize(Roles = Roles.AdminRole)]
        [HttpPost]
        public async Task<IActionResult> CreateRole([FromBody] CreateRoleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }

            var result = await _roleManager.CreateAsync(new IdentityRole(model.RoleName));

            if (!result.Succeeded)
                return BadRequest(result); …
Run Code Online (Sandbox Code Playgroud)

c# authorization asp.net-web-api .net-core asp.net-core

1
推荐指数
2
解决办法
1839
查看次数