繁体   English   中英

ASP.NET 核心:Fluent Api 关系配置

[英]ASP.NET Core : Fluent Api relationships configuration

网上有很多关于如何使用Fluent API的例子,但主要是展示如何配置两个模型之间的一种关系。 就我而言,我需要 2 个模型之间的 3 个关系。 如何使用 Fluent API 配置以下模型之间的关系?

public class Company
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int FinanceEstimateId { get; set; }
    public Estimate FinanceEstimate { get; set; }

    public int EnvironmentEstimateId { get; set; }
    public Estimate EnvironmentEstimate { get; set; }

    public int SelfEstimateId { get; set; }
    public Estimate SelfEstimate { get; set; }
}

public class Estimate
{
    public int Id { get; set; }
    public string Name { get; set; } // like: bad, good, excellent
    public float Value { get; set; } // like: 1,2,3
}

也许这为您指明了正确的方向。

对于 2 种配置,我会使用 go,例如:

public class CompanyConfiguration : IEntityTypeConfiguration<Company>
{
    public void Configure(EntityTypeBuilder<Company> builder)
    {
        builder.ToTable("Companies");

        builder
            .HasOne(x => x.EnvironmentEstimate)
            .WithMany()
            .HasForeignKey(x => x.EnvironmentEstimateId)
            .OnDelete(DeleteBehavior.NoAction);

        builder
            .HasOne(x => x.FinanceEstimate)
            .WithMany()
            .HasForeignKey(x => x.FinanceEstimateId)
            .OnDelete(DeleteBehavior.NoAction);

        builder
            .HasOne(x => x.SelfEstimate)
            .WithMany()
            .HasForeignKey(x => x.SelfEstimateId)
            .OnDelete(DeleteBehavior.NoAction);
    }
}

public class EstimateConfiguration : IEntityTypeConfiguration<Estimate>
{
    public void Configure(EntityTypeBuilder<Estimate> builder)
    {
        builder.ToTable("Estimates");
    }
}

你需要一个 DbContext:

public class MyDbContext : DbContext
{
    public DbSet<Company> Companies { get; set; } = null!;

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"CONNECTIONSTRING");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // applies the configuration (those IEntityTypeConfiguration<T> things)
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);
    }
}

我创建了一个控制台应用程序来演示用法

using var ctx = new MyDbContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

var company1 = new Company
{
    Name = "Name1",
    EnvironmentEstimate = new Estimate { Name = "EnvironmentEstimate1", Value = 1 },
    FinanceEstimate = new Estimate { Name = "FinanceEstimate1", Value = 2 },
    SelfEstimate = new Estimate { Name = "SelfEstimate1", Value = 3 }
};

var company2 = new Company
{
    Name = "Name2",
    EnvironmentEstimate = new Estimate { Name = "EnvironmentEstimate2", Value = 4 },
    FinanceEstimate = new Estimate { Name = "FinanceEstimate2", Value = 5 },
    SelfEstimate = new Estimate { Name = "SelfEstimate2", Value = 6 }
};

await ctx.Companies.AddAsync(company1);
await ctx.Companies.AddAsync(company2);
await ctx.SaveChangesAsync();

var result = await ctx.Companies.ToListAsync();

Console.WriteLine("Done");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM