Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure a one-to-one relation with some seed data for a owned type #27088

Closed
remus-corneliu opened this issue Jan 1, 2022 · 0 comments
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@remus-corneliu
Copy link

remus-corneliu commented Jan 1, 2022

Is it something that I'm missing in my configuraiton ?

I want to configure a one-to-one relation with some seed data for a owned type.

I've tried the following but with no success

Following is my code.

public class Beneficiary
{
    public long Id { get; set; }

    public string Name { get; set; }
}

public class BeneficiaryInvoice
{
    public string Number { get; set; }

    public DateTime IssueDate { get; set; }

    public BeneficiaryInvoice Invoice { get; set; }
}

    void IEntityTypeConfiguration<Beneficiary>.Configure(EntityTypeBuilder<Beneficiary> builder)
    {
        builder.HasKey(pk => pk.Id);

        builder.Property(pk => pk.Id)
            .UseIdentityColumn()
            .ValueGeneratedOnAdd();

        builder.Property(self => self.Name)
            .HasMaxLength(512)
            .IsRequired();

        builder
            .OwnsOne(self => self.Invoice,
                nav =>
                {
                    nav.ToTable("Invoices");
                    nav.Property(nav => nav.Number).HasMaxLength(16).IsRequired();
                    nav.Property(nav => nav.IssueDate).HasDefaultValueSql("getutcdate()");
                })
            .HasData(new
            {
                Id = (long)-1,
                IssueDate = DateTime.UtcNow,
                Number = "004"
            });

        builder
            .Navigation(self => self.Invoice)
            .IsRequired();

        builder.HasData(
            new Beneficiary
            {
                Id = -1,
                Name = "abc",                    
            });
    }
The seed entity for entity type 'Beneficiary' cannot be added because no value was provided for the required property 'Name'.

Whic I don't know why it complains about because I clearly set the name property value.

If however I make the name as optional by setting isRequired to false (but it needs to be required)

The seed entity for entity type 'Beneficiary' cannot be added because another seed entity with the key value 'Id:-1' has already been added.

I don't understand what I'm missing.

EF Core version: "5"

Database provider: SqlServer

PackageReferences:

  • Microsoft.EntityFrameworkCore, version :5.0.9
  • Microsoft.EntityFrameworkCore.SqlServer, version :5.0.9

Target: dotnet Core 5

Operating system: Windows

IDE: Visual Studio 2019

@roji roji added the closed-no-further-action The issue is closed and no further action is planned. label Jan 1, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants