-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support single configuration for all uses of an owned type, shared type, or some arbitrary base class/interface #6787
Comments
We don't have the equivalent functionality in EF Core yet, but you can write something like this (less pretty, but does the same thing). protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes().Where(e => typeof(IEntity).IsAssignableFrom(e.ClrType)))
{
modelBuilder.Entity(entity.Name).HasKey(nameof(IEntity.Key));
}
} |
The workaround mentioned above doesn't seem to be working for inherited property definitions. I have the following statement in my
I'd expect this produce the following line in the migration for each
|
@chrispickford It works for me in a simple test: public interface IEntity
{
Guid Id { get; set; }
}
public class Vehicle : IEntity
{
public Guid Id { get; set; }
public string VehicleIdentificationNumber { get; set; }
}
public class VehiclePrototype : IEntity
{
public Guid Id { get; set; }
public DateTimeOffset? PressReleaseAt { get; set; }
}
public class BloggingContext : DbContext
{
private static readonly LoggerFactory Logger
= new LoggerFactory(new[] { new ConsoleLoggerProvider((_, __) => true, true) });
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(Logger)
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Vehicle>();
modelBuilder.Entity<VehiclePrototype>();
foreach (var entity in modelBuilder.Model.GetEntityTypes()
.Where(e => typeof(IEntity).IsAssignableFrom(e.ClrType)))
{
modelBuilder.Entity(entity.Name).Property(nameof(IEntity.Id))
.IsRequired().HasDefaultValueSql("NEWID()");
}
}
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Add(new Vehicle());
context.Add(new VehiclePrototype());
context.SaveChanges();
}
}
} |
@ajcvickers Apologies, in my haste I constructed an incorrect example. The
|
@chrispickford Still works for me. Can you please file a new issue including a runnable project/solution or complete code listing that demonstrates the behavior you are seeing. |
Consider also query filters--see #17434 |
Would really love to see this, are there any current plans to implement this a future version of EFCore? Currently using reflection to configure my interface derived entities, but like the Framework interface configuration way more. |
@visschersm This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 6.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
Proposed API design: #12229 (comment) |
Consider query filters when working on this. See #10257. |
Previous non core version of Entity Framework had something like this:
I'm curious if this is planned for core version to?
I tried to implement something similar with reflection. https://gist.github.com/matjazmav/2e7335c12c55c4f6692b637bccf651bb There is only one problem with this implementation, casting
object
toEntityModelBuilder<TEntity>
(this is marked with// TODO
in the gist snippet). Anyone wan't to help me solve this?Thanks!
The text was updated successfully, but these errors were encountered: