EF Core FK Constraint on Alternate Key

asp.net-core c# ef-fluent-api entity-framework-core

Question

Hi I have an entity as below

public class SalaryTransactionAudit
{
      public long SalaryTransactionAuditId { get; set; }
      public Guid TransactionBatchId { get; set; }
      public DateTime UploadedDate { get; set; }
      public string UploadedBy { get; set; }
      public long SalaryTransactionStatusId { get; set; }
}

The above entity has primary key on SalaryTransactionAuditId and Alternate Key on TransactionBatchId as below

public class SalaryTransactionAuditConfiguration : IEntityTypeConfiguration<SalaryTransactionAudit>
{
        public void Configure(EntityTypeBuilder<SalaryTransactionAudit> builder)
        {
            builder.ToTable("SalaryTransactionAudit");
            builder.HasKey(e => e.SalaryTransactionAuditId);
            builder.HasAlternateKey(e => e.TransactionBatchId);
        }
 }

And I also have this entity

public class SalaryTransaction
 {
        public long SalaryTransactionId { get; set; }
        public Guid TransactionBatchId { get; set; }
        public long EmployeeId { get; set; }
        public int AnnualSalary { get; set; }
        public int SuperRate { get; set; }
        public int PaymentPeriodYear { get; set; }
        public int PaymentPeriodMonth { get; set; }

        public Employee Employee { get; set; }
   }

How do I configure (using fluent API) for FK Constraint on TransactionBatchId something like below

public class SalaryTransactionConfiguration : IEntityTypeConfiguration<SalaryTransaction>
{
        public void Configure(EntityTypeBuilder<SalaryTransaction> builder)
        {
            builder.ToTable("SalaryTransaction");
            builder.HasKey(e => e.SalaryTransactionId);
            builder.HasForeignKey(e => e.TransactionBatchId );

        }
} 
1
1
4/15/2019 10:56:38 AM

Accepted Answer

Foregn / principal keys of a relationship are configured via relationship builders HasForeignKey / HasPrincipalKey fluent API.

It would be good if you add navigation property at least to one of the related entities. But with the current model, and assuming the relationship is one-to-many, it could be something like this:

Inside SalaryTransactionConfiguration class

builder
    .HasOne<SalaryTransactionAudit>()
    .WithMany()
    .HasForeignKey(e => e.TransactionBatchId)
    .HasPrincipalKey(e => e.TransactionBatchId);

or inside SalaryTransactionAuditConfiguration class

builder
    .HasMany<SalaryTransaction>()
    .WithOne()
    .HasForeignKey(e => e.TransactionBatchId)
    .HasPrincipalKey(e => e.TransactionBatchId);

Relationships always have two ends, so when using entity type configuration classes, put the configuration in either of them, but not both (to avoid redundant or confliction configuration for one and the same relationship). Also make sure to use Has / With methods correctly, i.e. with or without navigation property expression depending of whether it exists or not.

6
4/15/2019 10:46:39 AM

Popular Answer

You can try to use DataAnnotation something like this:

public Guid TransactionBatchId { get; set; }

[ForeignKey("TransactionBatchId")]  
public SalaryTransactionAudit SalaryTransactionAudit { get; set; }

See also: MSDN



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow