I am using EF Core 2.1
How to map one-to-one relationship in EF Core. I have Customer
& Course
domain entity where one customer will have one Course.
This is my how my Customer & CoursePOCO looks like.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string CouseName { get; set; }
public virtual Customer Customer { get; set; }
}
I am using FluentAPI.
public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Parent> builder)
{
builder.HasKey(x => x.Customer.Id) //not allowing -> throws error
//The properties expression 'x => Convert(x.Customer.Id, Object)' is not valid.
// The expression should represent a simple property access: 't => t.MyProperty'.
// When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
// Parameter name: propertyAccessExpression
}
}
Since it's one to one relation, I don't want to create an extra key in Contact (FK -CustomerId),
Primary requirement:-
I want to define Id
(in Course) as PK + FK
& in this relation Customer is parent entity.
Like if I was Configuration based Migration, I would do as follows:-
public class Course
{
[Key]
[ForeignKey("Customer")]
public int Id { get; set; }
public string Name { get; set; }
public virtual Customer Customer { get; set; }
}
same thing I would like to do using Fluent API in EF Core??
Thanks!!