エンティティフレームワーク4コードの複合プライマリキーを最初にマップするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2733254

質問

私は最初にEF4コードを把握し、これまでのところそれが好きです。しかし、複合プライマリキーを使用してエンティティをテーブルにマッピングするのに問題があります。

私が試した構成は次のように見えます:

public SubscriptionUserConfiguration()

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

この例外は、エンティティタイプの「サブスクリプショット使用者」のキーを推測できません。

何が足りないの?

役に立ちましたか?

解決

使用することもできます

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

編集:

私が見つけた制限の1つは、以下が機能しないことです。

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

また

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

それでは、参加テーブルに外国の鍵で構成される主要なキーがあるエンティティをどのように設定しますか?

他のヒント

次のエンティティを使用して、段階的に説明しようとします

public class Account
{
    public int AccountId1 { get; set; }
    public int AccountId2 { get; set; }
    public string Description { get; set; }
}
  1. から派生したクラスを作成します EntityTypeConfiguaration<TEntity> 慣習をオーバーライドするためのオブジェクト

    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. から派生したクラスを作成するとき DbContext オブジェクト、オーバーライド OnModelCreating メソッドと新しいを追加します AccountEntityTypeConfiguration モデルビルダーの構成にオブジェクトします。

    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());
        }
    
    }
    

それがあなたに役立つことを願っています!

使用することもできます Column 属性

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

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

それを解決しました:私はアイデンティティではなく、haskeyを使用する必要があります。これは機能します:

public SubscriptionUserConfiguration()
{
     HasKey(u => u.SubscriptionID);
     HasKey(u => u.UserName);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top