gpt4 book ai didi

c# - 如何防止创建迁移并将它们应用于 Entity Framework Core 6 中的指定 dbContext?

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

我在端使用了两个数据库上下文,指向相同的数据库模式。

ReadDbContextWriteDbContext 分别定义了自己的 模型。

由于关系、表名和最后的数据库在这两个上下文配置中是相同的,所以只有其中一个可以搭建数据库。

有什么方法可以禁用为指定的 db conext 应用创建的迁移的能力吗?更进一步,是否有可能甚至禁止创建迁移?

我尝试添加 Database.SetInitializer<TContext>(null)到 DbContext 构造函数,但这在 EF Core 6 中似乎不起作用。

为了更好地理解,您可以查看下面的代码。

ReadDbContext

internal sealed class ReadDbContext : DbContext
{
public DbSet<UserReadModel> Users => Set<UserReadModel>();
public DbSet<RoleReadModel> Roles => Set<RoleReadModel>();
public DbSet<PermissionReadModel> Permissions => Set<PermissionReadModel>();

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

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("user-manager");

var configuration = new ReadConfiguration();

modelBuilder.ApplyConfiguration<UserReadModel>(configuration);
modelBuilder.ApplyConfiguration<RoleReadModel>(configuration);
modelBuilder.ApplyConfiguration<PermissionReadModel>(configuration);
}
}

WriteDbContext

internal sealed class WriteDbContext : DbContext
{
public DbSet<User> Users => Set<User>();
public DbSet<Role> Roles => Set<Role>();
public DbSet<Permission> Permissions => Set<Permission>();

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

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("user-manager");

var configuration = new WriteConfiguration();

modelBuilder.ApplyConfiguration<User>(configuration);
modelBuilder.ApplyConfiguration<Role>(configuration);
modelBuilder.ApplyConfiguration<Permission>(configuration);
}
}

读取配置

internal sealed class ReadConfiguration : IEntityTypeConfiguration<UserReadModel>, IEntityTypeConfiguration<RoleReadModel>,
IEntityTypeConfiguration<PermissionReadModel>
{
private readonly ValueConverter<UserNameReadModel, string> _userNameConverter = new(un => un.ToString(), un => new UserNameReadModel(un));

public void Configure(EntityTypeBuilder<UserReadModel> builder)
{
builder.ToTable("Users");
builder.HasKey(u => u.Id);

builder
.HasMany(u => u.Roles)
.WithMany(r => r.Users)
.UsingEntity("UsersRoles");

builder
.Property(u => u.Name)
.HasConversion(_userNameConverter!);
}

public void Configure(EntityTypeBuilder<RoleReadModel> builder)
{
builder.ToTable("Roles");
builder.HasKey(r => r.Id);

builder
.HasMany(r => r.Permissions)
.WithMany(p => p.Roles)
.UsingEntity("RolesPermissions");
}

public void Configure(EntityTypeBuilder<PermissionReadModel> builder)
{
builder.ToTable("Permissions");
builder.HasKey(p => p.Id);
}
}

写入配置

internal sealed class WriteConfiguration : IEntityTypeConfiguration<User>, IEntityTypeConfiguration<Role>,
IEntityTypeConfiguration<Permission>
{
private readonly ValueConverter<UserId, Guid> _userIdConverter = new(u => u.Value, u => u);
private readonly ValueConverter<RoleId, Guid> _roleIdConverter = new(r => r.Value, r => r);
private readonly ValueConverter<UserName, string> _userNameConverter = new(un => un.ToString(), un => UserName.Create(un));
private readonly ValueConverter<RoleName, string> _roleNameConverter = new(rn => rn.ToString(), rn => RoleName.Create(rn));
private readonly ValueConverter<Email, string> _emailConverter = new(e => e.ToString(), e => Email.Create(e));
private readonly ValueConverter<Password, string> _passwordConverter = new(p => p.ToString(), p => Password.Create(p));

public void Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("Users");
builder.HasKey(u => u.Id);
builder
.Property(r => r.Id)
.HasConversion(_userIdConverter);

builder
.Property(typeof(UserName), "_name")
.HasConversion(_userNameConverter)
.HasColumnName("Name");

builder
.Property(typeof(Email), "_email")
.HasConversion(_emailConverter)
.HasColumnName(nameof(Email));

builder
.Property(typeof(Password), "_password")
.HasConversion(_passwordConverter)
.HasColumnName(nameof(Password));

builder
.HasMany(typeof(Role), "_roles")
.WithMany("_users")
.UsingEntity("UsersRoles");
}

public void Configure(EntityTypeBuilder<Role> builder)
{
builder.ToTable("Roles");
builder.HasKey(r => r.Id);
builder
.Property(r => r.Id)
.HasConversion(_roleIdConverter);

builder
.Property(typeof(RoleName), "_name")
.HasConversion(_roleNameConverter)
.HasColumnName("Name");

builder.HasMany(typeof(Permission), "_permissions");
}

public void Configure(EntityTypeBuilder<Permission> builder)
{
builder.ToTable("Permissions");
builder.Property<Guid>("Id");
builder.Property<string>("Name");
}
}

最佳答案

您可以从迁移中排除:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>().ToTable("ApplicationUsers", t => t.ExcludeFromMigrations());
}

关于c# - 如何防止创建迁移并将它们应用于 Entity Framework Core 6 中的指定 dbContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71224365/

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