Is there a way to use interfaces as fields of a class in EF Core (CodeFirst, last version to day 5.0)?
say
public class Order
{
public int Id { get; set;}
public IUser CreatedBy { get; set; }
}
Is there a way to use interfaces as fields of a class in EF Core (CodeFirst, last version to day 5.0)?
say
public class Order
{
public int Id { get; set;}
public IUser CreatedBy { get; set; }
}
You can use interfaces for navigation properties, that are not exist in db. But for the Db columns you need the concrete classes.
In your case you have two way :
public class Order
{
[Key]
public int Id { get; set;}
[ForeignKey]
public int UserId CreatedBy { get; set; }
}
This way you can save UserId to Db and reuse it. This way is prefered.
Another way :
public class Order
{
[Key]
public int Id { get; set;}
[NotMapped]
public IUser CreatedBy { get; set; }
}
But this way IUser can be used as long as an Order instance exist im memory.