【发布时间】:2014-12-17 18:10:17
【问题描述】:
假设我们在数据库中有一个表 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 表会在应用程序的其他部分引入复杂性,我无法理解,这就是我们尝试这种方式的原因。
Code First 类 Car 和 Metadata 如下所示
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; }
}
Car 和 Metadata 的配置类
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() 时,确保实现这些属性的最优雅、最有效的方法是什么?
【问题讨论】:
-
看看指定
[ForeignKey]属性? (或者,您可以在 fluent 定义中指定导航属性)。 -
现实生活中有多少种不同的元数据?
-
@GertArnold 大约 30 岁。
标签: c# entity-framework entity-framework-6