- Microsoft.EntityFrameworkCore 10.0.3
- Microsoft.EntityFrameworkCore.Design 10.0.3
- Npgsql.EntityFrameworkCore.PostgreSQL 10.0.0
Код: Выделить всё
-- 1. create schema
create schema if not exists trgt;
-- 2. create table trgt.target
create table trgt.target (
id serial primary key,
name varchar(255) not null
);
-- 3. create table trgt.target_request
create table trgt.target_request (
id serial primary key,
target_id integer not null,
is_active boolean not null,
constraint fk_target_request_target
foreign key (target_id)
references trgt.target(id)
on delete cascade
);
-- 4. ensure only one active request per target
create unique index ux_target_request_active_per_target
on trgt.target_request (target_id)
where is_active = true;
Код: Выделить всё
dotnet ef dbcontext scaffold "Host=localhost;Database=UniqueIndexScaffold;Username=postgres;Password=postgres" Npgsql.EntityFrameworkCore.PostgreSQL -c TrgtDbContext -o Models --schema trgt --force --verbose --data-annotations --no-onconfiguring
Код: Выделить всё
public partial class TrgtDbContext : DbContext
{
public TrgtDbContext(DbContextOptions options)
: base(options)
{
}
public virtual DbSet Targets { get; set; }
public virtual DbSet TargetRequests { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(entity =>
{
entity.HasKey(e => e.Id).HasName("target_pkey");
});
modelBuilder.Entity(entity =>
{
entity.HasKey(e => e.Id).HasName("target_request_pkey");
entity.HasIndex(e => e.TargetId, "ux_target_request_active_per_target")
.IsUnique()
.HasFilter("(is_active = true)");
entity.HasOne(d => d.Target).WithOne(p => p.TargetRequest).HasConstraintName("fk_target_request_target");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Код: Выделить всё
[Table("target", Schema = "trgt")]
public partial class Target
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("name")]
[StringLength(255)]
public string Name { get; set; } = null!;
[InverseProperty("Target")]
public virtual TargetRequest? TargetRequest { get; set; }
}
Если мы удалим уникальный индекс ux_target_request_active_per_target и снова создадим базу данных, Target.cs будет содержать свойство public virtual ICollection TargetRequests, что и ожидается. Мы также ожидаем, что именно это свойство будет присутствовать, когда индекс ux_target_request_active_per_target существует.
Это ошибка в scaffolder? Есть ли обходные пути?
Подробнее здесь: https://stackoverflow.com/questions/799 ... pping-inst
Мобильная версия