Ich benutze Entity Framework 6 mit Table-Per-Type-Vererbung Ansatz.Woher kennt EF6 abgeleiteten Typ?
Tabellen wie folgt aussehen (nur ein Beispiel):
ConfigurationKeys
ID - int, not null, auto increment, primary
ConfigurationKeyType - int, not null
Key - varchar(50), not null
StringConfigurationKeys
ID - int, not null, primary (foreign key to ConfigurationKey.ID)
StringValue - varchar(50), not null
IntConfigurationKeys
ID - int, not null, primary (foreign key to ConfigurationKey.ID)
IntValue - int, not null
und die folgenden Klassen Struktur:
public enum ConfigurationKeyType
{
StringConfigurationKey = 0,
IntConfigurationKey = 1
}
[Table("ConfigurationKeys")]
public class ConfigurationKey
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public ConfigurationKeyType ConfigurationKeyType { get; set; }
public string Key { get; set; }
}
[Table("StringConfigurationKeys")]
public class StringConfigurationKey : ConfigurationKey
{
public string StringValue { get; set; }
}
[Table("IntConfigurationKeys")]
public class IntConfigurationKey : ConfigurationKey
{
public int IntValue { get; set; }
}
Keine andere Konfigurationen und Modellbauer Zuordnungen vorgenommen werden.
Nun, wenn ich die folgende Abfrage tun:
var keys = context.ConfigurationKeys.ToArray();
Entity Framework gibt einen Array von abgeleiteten Typen.
i.e., keys[0]
kann IntConfigurationKey
und keys[1]
vom Typ StringConfigurationKey
sein. Ich kann es sicher umwandeln und auf Eigenschaften abgeleiteter Typen zugreifen.
Es ist ein großartiges Feature und ich liebe es, aber ich würde gerne wissen, wie es funktioniert, um diese Funktionalität in Zukunft zu erhalten.
Verwendet es enum oder sucht es nach einer Entität mit der gleichen ID in allen Tabellen?
Das ist die perfekte und vollständige Antwort auf meine Frage :) Danke. –