Reputation: 531
I am using Microsoft.EntityFrameworkCore.Sqlite v1.0.1. This is my entity:
public class Category
{
public int CategoryId { get;set;}
public Category ParentCategory { get; set; }
public int? ParentCategoryId { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
}
So i need to configure ParentCategory as an optional property. How can i do this in EF core? This is my fluent mapping until now:
modelBuilder.Entity<Category>(e =>
{
e.HasKey(c => c.CategoryId);
e.Property(c => c.DateCreated).ValueGeneratedOnAdd();
e.Property(c => c.DateModified).ValueGeneratedOnAddOrUpdate();
});
Upvotes: 3
Views: 2734
Reputation: 30045
Your ParentCategoryId
is already optional based on the fact that it is nullable. If it wasn't nullable, it would be required. You don't need to configure it further.
Note, you don't need to configure CategoryId
as the Key for Category
. It will be configured as the key because it already follows the naming convention for an entity key.
Upvotes: 3