Bruno Bernardes
Bruno Bernardes

Reputation: 23

EF CORE - Create one-to-many map

I'm trying to create a one-to-many map, I tried a lot ways to do that, but I just I haven't figured it out yet :/

I have 2 entites, Wallet and Transfer, I want to add in Transfer a FK WalletId, so one transfer has just one wallet, but a wallet can be related to more than one transfer.

Wallet.cs -

public class Wallet
    {

        public int Id { get; private set; }
        public decimal Balance { get; private set; }
    }

Transfer.cs -

public class Transfer
    {
        #region Properties
        public int Id { get; private set; }
        public decimal Value { get; private set; }
        public DateTime? TransferDate { get; private set; }
        public DateTime RegisterDate { get; private set; }
        public ETransferType TransferType { get; private set; }
    }

WalletMap.cs -

public class WalletMap : IEntityTypeConfiguration<Wallet>
    {
        public void Configure(EntityTypeBuilder<Wallet> builder)
        {
            builder.HasKey(x => x.Id);
            builder.Property(x => x.Balance).HasColumnType("Money").IsRequired();
        }
    }

TransferMap.cs -

public class TransferMap : IEntityTypeConfiguration<Transfer>
    {
        public void Configure(EntityTypeBuilder<Transfer> builder)
        {
            builder.HasKey(x => x.Id);
            builder.Property(x => x.Value).HasColumnType("Money").IsRequired();
            builder.Property(x => x.TransferDate);
            builder.Property(x => x.RegisterDate).IsRequired();
            builder.Property(x => x.TransferType).IsRequired();
        }
    }

Upvotes: 0

Views: 462

Answers (2)

jaabh
jaabh

Reputation: 1004

Add a foreign key and navigation property:

public class Wallet
{
    public int Id { get; set; }
    public decimal Balance { get; set; }
    public virtual ICollection<Transfer> Transfers { get; set; } // Navigation Property
}

public class Transfer
{
    pubic int Id { get; private set; }
    public decimal Value { get; private set; }
    public Datetime? TransferDate { get; private set; }
    //.....Remaining properties
    public int WalletId { get; set; }  //Foreign Key
    public virtual Wallet Wallet { get; set; }  //Reference Navigation
}

This will add the foreign key and navigation properties needed.

//using fluent api
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Transfer>().HasOne(x => x.Wallet)
        .WithMany(x => x.Transfers)
        .HasForeignKey(x => x.WalletId);
    modelBuilder.Entity<Wallet>.HasMany(x => x.Transfers)
        .WithOne();
}

Upvotes: 1

Rich
Rich

Reputation: 271

To Wallet.cs add:

public virtual ICollection<Transfer> Tranfers { get; set; }

To Transfer.cs add:

public virtual Wallet Wallet { get; set; }

To TransferMap.cs add in your Configure block:

builder.HasRequired(x => x.Wallet).WithMany(x => x.Transfers).Map(x => x.MapKey("WalletId")).WillCascadeOnDelete();

You can remove the WillCascadeOnDelete() if you have not configured a cascading delete in the database. This assumes you already have WalletId defined in your Transfers table in the database.

Upvotes: 1

Related Questions