كيف يمكنني تعيين مفتاح أساسي مركب في إطار الكيان 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 });

يحرر:

أحد القيود التي وجدتها هي أن ما يلي لا يعمل:

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