【问题标题】:Database Query with EF Core in 2.12.1 中使用 EF Core 的数据库查询
【发布时间】:2019-03-16 22:14:51
【问题描述】:

我刚刚创建了一个带有身份的全新核心 MVC 应用程序。我这辈子都不知道如何让实体框架进行数据库查询。

我有一个 ApplicationDBContext

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

每当我尝试使用它时,它只会告诉我我没有发送任何选项。

我正在尝试对我的 UserAddresses 表运行查询,并将我的 ASPNetUsers 表更改为 Users。

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        // Update the ApplicationUser Table to Users and change primary column to UserId
        builder.Entity<ApplicationUser>(entity =>
        {
            entity.ToTable("Users");
            entity.Property(e => e.Id).HasColumnName("UserId");
        });
    }

    public DbSet<UserAddress> UserAddresses { get; set; }

这是整个班级

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

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        // Update the ApplicationUser Table to Users and change primary column to UserId
        builder.Entity<ApplicationUser>(entity =>
        {
            entity.ToTable("Users");
            entity.Property(e => e.Id).HasColumnName("UserId");
        });
    }

    public DbSet<UserAddress> UserAddresses { get; set; }
}

如何连接到 DBContext 以使用 EF 运行查询

更新

我可以通过像这样的构造函数将我的_context 传递给控制器​​来获取它

private ApplicationDbContext _context;
    public HomeController(ApplicationDbContext context)
    {
        _context = context;
    }

【问题讨论】:

  • 如果将工具从 IdentityDbContext&lt;ApplicationUser&gt; 更改为 DbContext 会发生什么?
  • 您想运行什么查询?分享我们相关的代码,你从哪里得到这个错误。只看到配置,没找到你怎么调用_context

标签: c# database asp.net-core entity-framework-core


【解决方案1】:
using (var context = new ApplicationDbContext())
{
    var user = await context.UserAddresses.SingleAsync(x => x.Id == UserId);

    return user;
}

添加一个空的构造函数:

public ApplicationDbContext() {}

【讨论】:

  • InvalidOperationException:没有为此 DbContext 配置数据库提供程序。可以通过重写 DbContext.OnConfiguring 方法或在应用程序服务提供者上使用 AddDbContext 来配置提供者。如果使用 AddDbContext,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数。
  • 这是在我的配置服务services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  • 你能发布你的Startup.csProgram.cs吗?
【解决方案2】:
private ApplicationDbContext _context;
public HomeController(ApplicationDbContext context)
{
    _context = context;
}

// this will get the userId of currently loggedIn user
userId = User.Identity.GetUserId()

// this line get all the address of loggedIn user from UserAddresses table
_context.UserAddresses.Where(a => a.UserId == userId).ToList();

您还可以获取单个地址、第一个地址或最后一个地址等。

Startup.cs 中的配置

services.AddIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<AppIdentityDbContext>()
            .AddDefaultTokenProviders();

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    相关资源
    最近更新 更多