craig
craig

Reputation: 26262

Can a related entity be refrenced in IEntityTypeConfiguration<T> configure method?

I have two model classes that are related:

Owner:

[Table("owner")]
public class Owner
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public ICollection<Account> Accounts { get; set; }
}

Account:

[Table("account")]
public class Account
{
    public Guid Id { get; set; }
    public DateTime DateCreated { get; set; }
    public string AccountType { get; set; }

    [ForeignKey(nameof(Owner))]
    public Guid OwnerId { get; set; }
    public Owner Owner { get; set; }
}

I'd like to be able to seed data using a configuration (IEntityTypeConfiguration<T>):

public class OwnerConfiguration : IEntityTypeConfiguration<Owner>
{
    public void Configure(EntityTypeBuilder<Owner> builder)
    {
        builder.HasData
        (
            new Owner
            {
                Id = Guid.NewGuid(),
                Name = "Owner A",
                Address = "Address A"
            }
        );
    }
}

public class AccountConfiguration : IEntityTypeConfiguration<Account>
{
    public void Configure(EntityTypeBuilder<Account> builder)
    {
        builder.HasData
        (
            new Account
            {
                Id = Guid.NewGuid(),
                DateCreated = DateTime.Now,
                AccountType = "I"
                // Owner = Owner A?
            },
            new Account
            {
                Id = Guid.NewGuid(),
                DateCreated = DateTime.Now,
                AccountType = "II"
                // Owner = Owner A?
            }
        );
    }

}

How do I assign an Owner to an Account in the Configure method?

Upvotes: 1

Views: 163

Answers (1)

DavidG
DavidG

Reputation: 119076

If you are using Guid.NewGuid for the primary keys, then every time the seed method runs, it will create new objects. The safest way is to specify a fixed value for the PK which means you can use it across both account and owner. For example:

Create one or more constants for your entities:

private const string OwnerAId = "38059984-50ac-45bf-a96b-d07043780955";
private const string AccountAId = "e83d91a9-3022-4298-9c40-3c53c1fc0595";

Now use those constants where necessary:

public class OwnerConfiguration : IEntityTypeConfiguration<Owner>
{
    public void Configure(EntityTypeBuilder<Owner> builder)
    {
        builder.HasData
        (
            new Owner
            {
                Id = Guid.Parse(OwnerAId), //<----------
                Name = "Owner A",
                Address = "Address A"
            }
        );
    }
}

public class AccountConfiguration : IEntityTypeConfiguration<Account>
{
    public void Configure(EntityTypeBuilder<Account> builder)
    {
        builder.HasData
        (
            new Account
            {
                Id = Guid.Parse(AccountAId), //<----------
                DateCreated = DateTime.Now,
                AccountType = "I"
                OwnerId = Guid.Parse(OwnerAId) //<----------
            },
        //etc....

Upvotes: 1

Related Questions