小编Joe*_*ley的帖子

实体框架核心使用多个DbContexts

我有一个问题,当我尝试访问我的PartsDbContext中的字段时,我收到以下错误:

System.Data.SqlClient.SqlException:'无效的对象名'fieldName''

这似乎是因为我试图使我的PartsDbContext使用与我的ApplicationDbContext相同的数据库,该数据库与Identity一起使用.我需要知道如何设置第二个dbcontext以使用/使用/创建不同数据库的EF核心.

我已经尝试创建第二个连接字符串,但这会让我犯这个错误:

System.Data.SqlClient.SqlException:'无法打开登录请求的数据库"PartsDb".登录失败.用户'DESKTOP-4VPU567\higle'登录失败.'

这是我的代码:

appsettings.json

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-PrecisionCustomPC-b14db89e-86ad-4855-a17f-ac64a04339aa;Trusted_Connection=True;MultipleActiveResultSets=true",
    "PartsConnection":  "Server=(localdb)\\mssqllocaldb;Database=PartsDb"
},
"Logging": {
    "IncludeScopes": false,
    "LogLevel": {
        "Default": "Warning"
    }
}
Run Code Online (Sandbox Code Playgroud)

PartsDbContext.cs

public class PartsDbContext : DbContext
{
    public DbSet<PartsViewModels.Tower> Towers { get; set; }
    public DbSet<PartsViewModels.Motherboard> Motherboards { get; set; }

    public PartsDbContext(DbContextOptions<PartsDbContext> options)
        : base(options)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
         options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.AddEntityFramework()
        .AddDbContext<PartsDbContext>(options =>
          options.UseSqlServer(Configuration.GetConnectionString("PartsConnection")));

    services.AddMvc();

    services.AddAuthorization(options =>
    {
        options.AddPolicy("RequireAdminRole", policy …
Run Code Online (Sandbox Code Playgroud)

c# dbcontext entity-framework-core asp.net-core-1.1

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

EntityFramework Core自动生成密钥id属性

使用.Net Core MVC.我需要设置我的模型,以便EntityFrameworkCore将自动生成ID属性.我认为只需添加[Key]注释即可,但事实并非如此.事实上,当ID字段留空时会产生错误,这总是因为我将其隐藏在用户之外.

我需要它来代替每次为模型自动生成一个唯一的ID.我该怎么做呢?我的ID属性目前是类型int.

ExampleModel

public class ExampleModel
{
    [Key]
    public int ID { get; set; }
    public string SomeData { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

ExampleView

@model MyNamespace.Models.ExampleModel

<form asp-action="Create" asp-controller="ExampleModel">
    @Html.EditorForModel()
    <input type="submit" name="Submit" value="Create"/>
</form>
Run Code Online (Sandbox Code Playgroud)

ExampleController

public IActionResult Create ()
{
    return View();
}
public IActionResult Create (ExampleModel model)
{
    if (ModelState.IsValid)
    {
        _context.ExampleModels.Add(model);
        _context.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

c# data-annotations entity-framework-core asp.net-core-mvc asp.net-core

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

如何为MVC .Net Core应用程序创建一个管理员用户

我正在构建一个基本上是商店的网络应用程序,但我希望为网站管理员提供一种简单的方法来添加新产品.但是,我想限制网站的这一部分,以便只有管理员可以访问它.此刻我对其他用户没用.

如何使用管理员用户名和密码的任何人都可以访问这些页面,并且它会坚持知道他们已登录?我已经有一个接受用户输入的系统,然后继续管理页面,如果它是正确的.但问题是,如果有人决定直接转到Admin/AddProduct这样的页面.我需要我的应用程序知道他们还不允许访问AddProduct页面并将它们重定向回登录.

c# asp.net-mvc login asp.net-core

3
推荐指数
2
解决办法
6471
查看次数