Domanda

Mi piacerebbe usare un enum come chiave straniera in un codice-prima app.Dal momento che gli enum sono conservati come int, pensavo di poter usare l'attributo [Keykey] sulla proprietà Enum, ma getta questa eccezione:

The types of all properties in the Dependent Role of a referential constraint 
must be the same as the corresponding property types in the Principal Role
.

Ecco un esempio di ciò che sto cercando di fare:

public enum UserType
{
    Administrator = 1,
    Member = 2
}

public class User
{
    public int UserId { get; set; }
    public string Login { get; set; }

    [ForeignKey("TypeDetails")]
    public UserType Type { get; set;}

    public virtual MasterType TypeDetails { get; set; }
}

public class MasterType
{
    public int MasterTypeId { get; set; }
    public string Description { get; set; }
    ...
}
.

È possibile farlo o qualcosa di simile attraverso API o migrazioni fluenti?

Grazie

È stato utile?

Soluzione

Ecco quello che ho fatto in precedenza: https://www.nuget.org/Pacchetti / EF-Enum-to-Lookup

È un pacchetto NUEGET che fornisce un metodo che è possibile chiamare nel Seed (Inizializzatore e / o migrazioni) che creerà automaticamente tabelle di ricerca e aggiungano FKS dove viene utilizzato l'enum. Informazioni sull'utilizzo .

Divertiti :-) E fammi sapere se funziona per te (o chiunque altro per quella materia!)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top