Come faccio a mappare una chiave primaria composta in Entity Framework codice a 4 prima?
-
02-10-2019 - |
Domanda
sto alle prese con il codice EF4 prima, e piacerà finora. Ma sto avendo problemi di mappatura un'entità a una tabella con una chiave primaria composta.
La configurazione che ho provato si presenta così:
public SubscriptionUserConfiguration()
{
Property(u => u.SubscriptionID).IsIdentity();
Property(u => u.UserName).IsIdentity();
}
Il che genera questa eccezione: Impossibile dedurre una chiave per il tipo di entità 'SubscriptionUser'.
Che cosa mi manca?
Soluzione
Si potrebbe anche usare
HasKey(u => new { u.SubscriptionID, u.UserName });
Modifica:
Una limitazione che ho trovato è che il seguente non funzionano:
public ProjectAssignmentConfiguration()
{
HasKey(u => u.Employee.EmployeeId);
HasKey(u => u.Project.ProjectId);
}
o
public ProjectAssignmentConfiguration()
{
HasKey(u => new { u.Employee.EmployeeId, u.Project.ProjectId });
}
Quindi, come si fa a impostare un soggetto in cui il join tabella ha una chiave primaria che è composto di chiavi esterne?
Altri suggerimenti
cercherò di spiegare passo per passo, utilizzando il seguente Entity
public class Account
{
public int AccountId1 { get; set; }
public int AccountId2 { get; set; }
public string Description { get; set; }
}
-
Creare una classe derivata dalla Object
EntityTypeConfiguaration<TEntity>
per ignorare le convenzioniclass AccountEntityTypeConfiguration : EntityTypeConfiguration<Account> { public AccountEntityTypeConfiguration() { // The Key // The description of the HasKey Method says // A lambda expression representing the property to be used as the primary key. // If the primary key is made up of multiple properties then specify an anonymous type including the properties. // Example C#: k => new { k.Id1, k.Id2 } // Example VB: Function(k) New From { k.Id1, k.Id2 } this.HasKey(k => new { k.AccountId1, k.AccountId2 } ); // The Key // Maybe the key properties are not sequenced and you want to override the conventions this.Property(p => p.AccountId1).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); this.Property(p => p.AccountId2).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); this.Property(p => p.Description).IsRequired(); // This property will be required this.ToTable("Account"); // Map the entity to the table Account on the database } }
-
Quando creare la classe derivata dalla Object
DbContext
, overrideOnModelCreating
Metodo e aggiungere un nuovo oggettoAccountEntityTypeConfiguration
alle configurazioni del modello Builder.public class MyModelAccount : DbContext { public DbSet<Account> Accounts { get; set;} protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Add a new AccountEntityTypeConfiguration object to the configuration of the model, that will be applied once the model is created. modelBuilder.Configurations.Add(new AccountEntityTypeConfiguration()); } }
Spero che ti aiuta!
È possibile anche utilizzare l'attributo Column
public class UserProfileRole
{
[Key, Column(Order = 0)]
public int UserId { get; set; }
[Key, Column(Order = 1)]
public int RoleId { get; set; }
}
risolto: dovrei usare Haskey, non identità. Questo funziona:
public SubscriptionUserConfiguration()
{
HasKey(u => u.SubscriptionID);
HasKey(u => u.UserName);
}