0

I have two tables products and relationships.

Product:

public int Id {get; set;} //primary key
public string Name {get; set;}
public int Availability {get; set;}

Relationship:

public int Id {get; set;} //primary key
public string RelationshipName {get; set;}

Primary key of Relationship table to be set as foreign key in the Product. It is a one to one relationship. For entity framework core to generate migration, I have added a new property in both the tables as below

Product:

public int Id {get; set;} //primary key
public string Name {get; set;}
public int Availability {get; set;}

/// Added new property
public virtual Relationship Relationship {get; set;}

Relationship:

public int Id {get; set;} //primary key
public string RelationshipName {get; set;}

///Added new property
public virtual Product Product

RelationshipConfiguration is as below:

public void Configure(EntityTypeBuilder<Relationship> builder)
        {
            builder.ToTable("Relationship");
            builder.HasKey(x => x.Id);

            builder.Property(x => x.Id)
                .IsRequired()
                .HasMaxLength(DataConstants.KeyStringLength)
                .IsRequired()
                .IsFixedLength()
                .HasValueGenerator<SequentialGuidStringValueGenerator>();
        }

Product Configuration is as below:

public void Configure(EntityTypeBuilder<Product> builder)
        {
            builder.ToTable("Product");
            builder.HasKey(x => x.Id);

            builder.Property(x => x.Id)
                .IsRequired()
                .HasMaxLength(DataConstants.KeyStringLength)
                .IsRequired()
                .IsFixedLength()
                .HasValueGenerator<SequentialGuidStringValueGenerator>();
                
                //how do I set the foreign key configuration with relationship here
        }
    
    
  1. How to set the foreign key configuration with relationship in Product table? Need to set HasOne, WithOne, ForeignKey Relationship in Product Configuration? Should it be

    builder.HasOne(x => x.Relationship) .WithOne(x => x.Product) .HasForeignKey(x => x.Id);

// but this lead to having no property in Product to hold the foreign key value of Relationship

or

do I need to add another property in Product like

public int RelationshipId {get; set;}

and the configuration to be

builder.HasOne(x => x.Relationship)
               .WithOne(x => x.Product)
               .HasForeignKey<Product>(x => x.RelationshipId);

Can you please suggest what is the approach for configuring one to one relationship? And also adding a new property in each table the right approach?

Thanks in advance.

ChinnaR
  • 797
  • 3
  • 9
  • 24
  • Have you tried Google? First hit I get: https://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx using entity framework one to one relationship – Roar S. Aug 22 '20 at 16:46
  • Does this answer your question? [What does principal end of an association means in 1:1 relationship in Entity framework](https://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr) – Ben Cottrell Aug 22 '20 at 17:29
  • I do not see the options of hasoptional and withrequired in the config. I am using entity framework core 2.2, I could see options of hasone withone and hasforeignkey. Can you please suggest – ChinnaR Aug 22 '20 at 17:37

0 Answers0