gpt4 book ai didi

c# - Entity Framework Core 的 Cosmos 提供程序使用 DbContext 类名称创建容器

转载 作者:行者123 更新时间:2023-12-02 19:37:47 30 4
gpt4 key购买 nike

我在带有 Microsoft.EntityFrameworkCore.Cosmos 包的 asp net core 3.1 应用程序中使用 Azure Cosmos DB。在我的 OnModelCreating 方法的 AppContext 类中,我给每个 dbset ToContainer() 方法定义容器的名称,否则它将为容器提供 AppContext 名称。一切正常,但它仍然在 CosmosDb 中创建一个容器 AppContext,我该如何解决这个问题?请帮忙。下面是我的代码。

AppContext 类

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace CosmosApp.Context
{
public class AppContext : IdentityDbContext<AppUser>
{
public DbSet<Teacher> Teachers { get; set; }

public DbSet<Student> Students { get; set; }

public AppContext(DbContextOptions options)
: base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<IdentityUser>().ToContainer("Users");
modelBuilder.Entity<AppUser>().ToContainer("Users");
modelBuilder.Entity<IdentityUserRole<string>>().ToContainer("UserRoles");
modelBuilder.Entity<IdentityUserLogin<string>>().ToContainer("UserLogins");
modelBuilder.Entity<IdentityUserClaim<string>>().ToContainer("UserClaims");
modelBuilder.Entity<IdentityRole>().ToContainer("Roles");
modelBuilder.Entity<IdentityUserToken<string>>().ToContainer("UserTokens");

modelBuilder.Entity<Teacher>().ToContainer("Teachers");
modelBuilder.Entity<Student>().ToContainer("Students");
modelBuilder.Entity<Teacher>().HasNoDiscriminator();
modelBuilder.Entity<Student>().HasNoDiscriminator();
}
}
}

AppUser 类

using Microsoft.AspNetCore.Identity;

namespace CosmosApp.Entities
{
public class AppUser : IdentityUser
{
public string DisplayName { get; set; }
}
}

启动类

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppContext>(options =>
{
options.UseCosmos(
"https://localhost:8081/",
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
"TestDb");
});

services.AddControllers();

services.AddIdentity<AppUser, IdentityRole>(options => { })
.AddEntityFrameworkStores<AppContext>()
.AddDefaultTokenProviders();

services.AddAuthentication();
}

程序类

using System;
using CosmosApp.Context;
using CosmosApp.Entities;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using AppContext = CosmosApp.Context.AppContext;

namespace CosmosApp
{
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();

using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<AppContext>();
var userManager = services.GetRequiredService<UserManager<AppUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
context.Database.EnsureCreated();
Seed.SeedData(context, userManager, roleManager).Wait();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occured during migration");
}
}

host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}

包信息

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

现在您可以看到它正在创建未知的容器 AppContext enter image description here

最佳答案

使用HasDefaultContainer对于第一个实体和 ToContainer 在后续实体上。

 protected override void OnModelCreating( ModelBuilder builder ) {
builder.HasDefaultContainer("Users");
builder.Entity<User>().ToContainer("Users");
builder.Entity<Teacher>().ToContainer("Teachers");
builder.Entity<Student>().ToContainer("Students");

}

关于c# - Entity Framework Core 的 Cosmos 提供程序使用 DbContext 类名称创建容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60790797/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com