1

I have implemented the project of EntityFramework 6 into EntityFramework Core. I have to do migrate EF6 relationship pattern into EF core . I found some references below:

  1. Entity Framework Core zero-or-one to zero-or-one relation

but didn't get any ideas on required-optional relationship in EF-Core.

Sample1.cs:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<StudentAddress>()
                .HasRequired(x => x.Student)
                .WithOptional(x => x.StudentAddress);
}

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
}

public class StudentAddress
{
    public int StudentId { get; set; }
    public string State { get; set; }
    public virtual Student Student { get; set; }
}

Sample2.cs:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<StudentAddress>()
                .HasKey(sa => sa.StudentId);
    modelBuilder.Entity<StudentAddress>()
                .HasRequired(x => x.Student)
                .WithOptional(x => x.StudentAddress);
}

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
}

public class StudentAddress
{
    public int StudentId { get; set; }
    public string State { get; set; }
    public virtual Student Student { get; set; }
}

Kindly someone help me that how to do that in EF-Core using fluent api

Karthic G
  • 1,162
  • 1
  • 14
  • 31

1 Answers1

2

You can try this (Ref.: docs):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<StudentAddress>()
                .HasOne(x => x.Student)
                .WithOne(x => x.StudentAddress).IsRequired(false);
}

Alternatively, you could add a FK StudentAddressId in your Student Model an have the type be nullable int?

public class Student
{
    public int StudentId { get; set; }
    
    // This is the FK for StudentAddress.
    // Make it int? (nullable) if StudentAddress is optional
    public int? StudentAddressId { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
}

public class StudentAddress
{
    public int StudentId { get; set; }

    public virtual Student Student { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Since the FK is nullable, you don't need the IsRequired(false)
    modelBuilder.Entity<StudentAddress>()
                .HasOne(x => x.Student)
                .WithOne(x => x.StudentAddress);
}
dglozano
  • 6,369
  • 2
  • 19
  • 38
  • If Haskey specified in configuration what we do like "modelBuilder.Entity().HasKey(sa => sa.StudentId)". In the StudentAddress model has one more property as "public int StudentId { get; set; }"; – Karthic G Mar 08 '21 at 10:52
  • @KarthicG I am sorry could not understand what you meant in that comment – dglozano Mar 08 '21 at 10:56
  • I have edited my question with 2 samples. your answer is helpful for sample1.cs. I need to answer for sample2.cs if they used HasKey() method. – Karthic G Mar 08 '21 at 13:48
  • @KarthicG I think you are mixing up stuff. Why do you wanna make StudentId the PK in the StudentAddress table? StudentAddress should have StudentAddressId as PK, and perhaps StudentId as FK – dglozano Mar 08 '21 at 14:21
  • This is the one of my scenario for sample2.cs, so asked what is expected result in EF core? – Karthic G Mar 09 '21 at 05:35