question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I add default constructors to the entity classes?

See original GitHub issue

I would like to have the default constructor be like this if possible

If I can modify a template or something simple I will do it. I use the context often for the partial classes and EF 6 Core doesn’t have an easy way to access the entity context like I was able to in EF6. If I can add the constructor with the context simply then EF 6 will DI the parameters as needed.

public partial class MyDbContext 
{
    protected MyDbContext Context;
    public MyEntity(MyDbContext context)
    {
        Context = context;
    }
    
    /****
    Properties Code.....
    ***/
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikEJcommented, May 4, 2022

I do not think there is any compelling reason to do this, lazy loading is possible with EF Core using other means, and you can always create partial classes with the constructor as needed.

1reaction
tonysneedcommented, May 4, 2022

@glenlewisdd The pattern of injecting a DBContext into entities looks suspect to me. What is it you are trying to accomplish?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Need for Default Constructor in JPA Entities
In this quick tutorial, we'll learn about the JPA requirement for a default no-argument constructor in an Entity class.
Read more >
JPA: is the default constructor required to be empty?
When you are creating a "new" object, the constructor needs to produce an object that is sufficiently initialized to be used by normal...
Read more >
Default Constructor in Java – Class Constructor Example
In this article, we will talk about constructors, how to create our own constructors, and what default constructors are in Java.
Read more >
Entity types with constructors - EF Core
Using constructors to bind data with Entity Framework Core model. ... it will first call the default parameterless constructor and then set ......
Read more >
Chapter 4. Entity
Default or No-Arg Constructor. The JPA specification requires that all persistent classes have a no-arg constructor. This constructor may be public or protected ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found