Open
Description
Allow interfaces or base types to be mapped by specifying the type to use when materializing an instance of the interface or base class. For example, something like this:
public interface IBlog
{
int Id { get; set; }
ICollection<IPost> Posts { get; set; }
}
public interface IPost
{
int Id { get; set; }
IBlog Blog { get; set; }
}
public class Blog : IBlog
{
public int Id { get; set; }
public ICollection<IPost> Posts { get; set; }
}
public class Post : IPost
{
public int Id { get; set; }
public IBlog Blog { get; set; }
}
Currently this can be mapped like this:
public class BloggingContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasMany(e => (ICollection<Post>) e.Posts)
.WithOne(e => (Blog) e.Blog);
}
}
Instead, this would be mapped using something like this:
public class BloggingContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IBlog>().ImplementedBy<Blog>();
modelBuilder.Entity<IPost>().ImplementedBy<Post>();
}
}
Note that there is significant overlap here with #10789. For example, it should be possible to use a factory method for creating the implementation instance, in which case the factory rather than the explicit implementation types would be in the model.
This issue is also related to #757, except that here each interface/base type is constrained to only have one implementation type at runtime.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment