自定义导航属性

本文关键字:属性 导航 自定义 | 更新日期: 2023-09-27 18:34:22

>假设我们在数据库中有一个表Car,如下所示:

Id | Brand | Model | Color | Description
 1 |  23   |  6    |  005  | Beautiful car

另一个名为元数据的表包含有关汽车的不同信息。

Id | Type  | Key | Value
 1 | Brand |   6 | Ford
 2 | Brand |  22 | BMW
 3 | Brand |  23 | Audi
 4 | Model |   5 | Focus
 5 | Model |   6 | A4
 6 | Model |   7 | 325
 7 | Color | 005 | Black
 8 | Color | 019 | Blue

如您所见,组合 Type & Key 在表中应该是唯一的,并且可以被视为外键。

我完全理解规范化数据库的概念。在我们的例子中,规范化Metadata表会在应用程序中的其他部分引入复杂性,我无法进入,这就是我们尝试这样做的原因。

代码优先类CarMetadata如下所示

public class Car
{
    public int Id { get; set; }
    public string BrandId { get; set; }
    public string ModelId { get; set; }
    public string ColorId { get; set; }
}
public class Metadata
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}

CarMetadata的配置类

public class CarMap : EntityTypeConfiguration<Car>
{
    this.HasKey(t => t.Id);
    this.Property(t => t.BrandId).IsRequired().HasMaxLength(6);
    this.Property(t => t.ModelId).IsRequired().HasMaxLength(6);
    this.Property(t => t.ColorId).IsRequired().HasMaxLength(6);
    this.ToTable("Car");
    this.Property(t => t.Id).HasColumnName("Id");
    this.Property(t => t.BrandId).HasColumnName("Brand");
    this.Property(t => t.ModelId).HasColumnName("Model");
    this.Property(t => t.ColorId).HasColumnName("Color");
}
public class MetadataMap : EntityTypeConfiguration<Metadata>
{
    this.HasKey(t => t.Id);
    this.Property(t => t.Type).IsRequired().HasMaxLength(6);
    this.Property(t => t.Key).IsRequired().HasMaxLength(6);
    this.Property(t => t.Value).IsRequired().HasMaxLength(6);
    this.ToTable("Metadata");
    this.Property(t => t.Id).HasColumnName("Id");
    this.Property(t => t.Type).HasColumnName("Type");
    this.Property(t => t.Key).HasColumnName("Key");
    this.Property(t => t.Value).HasColumnName("Value");
}

还有DbContext

public class CarContext : DbContext
{
    public DbSet<Car> Cars { get; set; }
    public DbSet<Metadata> Metadata { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CarMap());
        modelBuilder.Configurations.Add(new MetadataMap());
    }
}

一种检索前 10 辆汽车的方法

public List<Car> GetAllCars()
{
    using (CarContext context = new CarContext())
    {
        return context.Cars.ToList();
    }
}

假设我将导航属性添加到 Car

public class Car
{
    public int Id { get; set; }
    public string BrandId { get; set; }
    public virtual Metadata Brand { get; set; }
    public string ModelId { get; set; }
    public virtual Metadata Model { get; set; }
    public string ColorId { get; set; }
    public virtual Metadata Color { get; set; }
} 

确保在调用 GetAllCars(( 时实现这些属性的最优雅、最有效的方法是什么?

自定义导航属性

我认为在这种情况下您不能拥有"真正的"导航属性,因为我不明白如何在数据库中定义这种关系和/或使用流畅的 API 在代码中映射它。(有人可能会在这里纠正我(。

在这种情况下,我会将它们定义为未映射并在您的GetAllCars()方法中填充它们。因此,Car类如下所示:

public class Car
{
    public int Id { get; set; }
    public string BrandId { get; set; }
    [NotMapped]
    public Metadata Brand { get; set; }
    public string ModelId { get; set; }
    [NotMapped]
    public Metadata Model { get; set; }
    public string ColorId { get; set; }
    [NotMapped]
    public Metadata Color { get; set; }
}

并且,方法是:

public List<Car> GetAllCars()
{
    using (CarContext context = new CarContext())
    {
        var returnList = new List<Car>();
        foreach(var car in context.Cars)
        {
            car.Brand = context.Metadata
                               .Where(x => x.Key == car.BrandId && x.Type == "Brand")
                               .FirstOrDefault();
            car.Model = context.Metadata
                               .Where(x => x.Key == car.ModelId && x.Type == "Model")
                               .FirstOrDefault();
            car.Color = context.Metadata
                               .Where(x => x.Key == car.ColorId && x.Type == "Color")
                               .FirstOrDefault();
            returnList.Add(car);
        }
        return returnList;
    }
}