Devo memorizzare coppie chiave / valore di entità sicura in EF Core.
Tipi di valori chiave
// KeyValuePair<string,bool>
// KeyValuePair<string,int>
// KeyValuePair<string,string>
// KeyValuePair<string,float>
// KeyValuePair<string,DateTime>
Entità
public class MyEntity
{
public int Id { get; set; }
public List<KeyValuePair<string,T>> Attributes { get; set; }
}
Altrove
{
…
dbContext.MyEntity.Add(new KeyValuePair<string,bool>("Active", true);
dbContext.MyEntity.Add(new KeyValuePair<string,string>("Customer", "Microsoft");
dbContext.MyEntity.Add(new KeyValuePair<string,string>("Street", "1 Microsoft Way");
dbContext.MyEntity.Add(new KeyValuePair<string,float>("Rating",5.0);
}
Come può essere realizzato in EF Core?
So che è un po 'tardi per una risposta, ma ho avuto lo stesso problema nel tentativo di persistere coppie di valori chiave. L'unico modo che ho trovato è stato quello di scriverli con forza, poiché EF non ha idea di come perseguire l'entità KeyValue.
Ecco il mio modello:
public class Payment
{
//-- Properties
public int PaymentId { get; set; }
public decimal Amount { get; set; }
public string Currency { get; set; }
public virtual List<PaymentField> PaymentFields { get; set; } = new List<PaymentField>();
}
public class PaymentField
{
//-- Properties
[Key]
public int PaymentFieldId { get; set; }
// References master table
public int PaymentId { get; set; }
public virtual Payment Payment { get; set; }
public virtual List<PaymentFieldItem> PaymentFieldItems { get; set; } = new List<PaymentFieldItem>();
}
public class PaymentFieldItem
{
public int PaymentFieldItemId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public int PaymentFieldId { get; set; }
public virtual PaymentField PaymentField { get; set; }
}
Spero che tu possa adattarti alle tue esigenze.