Frage

Ich bin in dem Griff EF4 Code immer zuerst, und es so weit zu mögen. Aber ich habe Probleme beim Mapping eines Unternehmen an einen Tisch mit einem zusammengesetzten Primärschlüssel.

Die Konfiguration, die ich wie folgt aussieht versucht haben:

public SubscriptionUserConfiguration()

    {
                Property(u => u.SubscriptionID).IsIdentity();
                Property(u => u.UserName).IsIdentity();
    }

Welche diese Ausnahme auslöst: Es kann kein Schlüssel für Entitätstyp SubscriptionUser 'schließen.

Was bin ich?

War es hilfreich?

Lösung

Sie können auch verwenden

HasKey(u => new { u.SubscriptionID, u.UserName });

Edit:

Eine Einschränkung ich gefunden habe, ist, dass die folgende nicht funktionieren:

public ProjectAssignmentConfiguration()
{
    HasKey(u => u.Employee.EmployeeId);
    HasKey(u => u.Project.ProjectId);
}

oder

public ProjectAssignmentConfiguration()
{
    HasKey(u => new { u.Employee.EmployeeId, u.Project.ProjectId });
}

Wie richten Sie eine Entität, wo die Join-Tabelle einen Primärschlüssel hat, die von Fremdschlüssel zusammengesetzt ist?

Andere Tipps

Ich werde versuchen, es für Schritt, mit dem folgende Entity

Schritt zu erklären,
public class Account
{
    public int AccountId1 { get; set; }
    public int AccountId2 { get; set; }
    public string Description { get; set; }
}
  1. Erstellen Sie eine Klasse aus dem EntityTypeConfiguaration<TEntity> Objekt abgeleitet, die Konventionen außer Kraft zu setzen

    class 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
        }
    }
    
  2. Wenn die Klasse erstellen, die von dem DbContext Objekt, überschreiben OnModelCreating Methode und fügen Sie ein neues AccountEntityTypeConfiguration Objekt zu den Konfigurationen des Modells 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());
        }
    
    }
    

Hope hilft es Ihnen!

Sie können auch das Column Attribut

public class UserProfileRole
{
    [Key, Column(Order = 0)]
    public int UserId { get; set; }

    [Key, Column(Order = 1)]
    public int RoleId { get; set; }
}

es gelöst: Ich sollte Haskey verwenden, nicht Identität. Dies funktioniert:

public SubscriptionUserConfiguration()
{
     HasKey(u => u.SubscriptionID);
     HasKey(u => u.UserName);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top