-
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
Add API to configure construction of entity type instances #10789
Comments
Is there any workaround to prevent parameterized constructors from being used until it's configurable? I have creation events in my constructors and they are getting raised every time the objects are queried. |
@xKloc Starting with preview2, if there are both parameterized and empty constructors, then EF will choose the empty one. See #10852 |
There is a note in the documentation of the Entity types with constructors section that as some point the following feature will be implemented:
Is this issue tracking this feature? One of my use cases would be to have the ability to publish some interesting events that happen within an entity
|
Support for ctor injecting an ILogger in domain entities would be a great addition. |
@AndriySvyryd Not sure it would be easy to do this with the API surface we have now. But it's a nice idea--maybe I'll add it as one of the built-in services we support. |
@AndriySvyryd @thomasdc Filed #20203 |
Wouldn't it be possible to allow injecting IServiceProvider and therefore make all services available to be grabbed by the entities? Sure it sounds like a potential to apply many anti-patterns but the decision to use it could be left up to the user. |
Would love to see something like: modelBuilder.Entity<Blog>().HasConstructor((string name, string author) => Blog.Create(name, author)); In the meantime, a NotMapped/Ignore attribute or a MappedConstructor attribute might be a quick win to avoid awkward workarounds for constructor selection. |
I'd like to propose a factory style plug-in implementation: I suggest to add a new method to The proposed public class DbContextOptionsBuilder : IDbContextOptionsBuilderInfrastructure
{
...
public virtual DbContextOptionsBuilder UseConstructorFactory(IConstructorFactory constructorFactory);
} The public interface IConstructorFactory
{
IEntityFactory<TEntity> UseEntityFactory<TEntity>();
} The public interface IEntityFactory<out TEntity>
{
public ConstructorInfo FindConstructor(DataRow dataRow);
public TEntity InvokeConstructorAndInitializers(ConstructorInfo constructor, DataRow dataRow);
public IEnumerable<TEntity> CreateObjects(DataTable dataTable);
} (The above interface definition is based on I created a generic sample implementation of https://github.com/SetTrend/EFC-Constructor-MRE My sample implementation comprises the following .NET Core 8.0 projects:
|
Issue #10703 covered the ability of EF to call a parameterized constructor. However, the constructor to use is not configurable. This issue is about adding fluent API and/or annotations to do this and also:
The text was updated successfully, but these errors were encountered: