Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must I have a parameterless constructor for Code First / Entity Framework

Tags:

This is more of a question of "Why we do things" as my actual problem was solved but I don't know why.

I was dealing with the following code inside my CountyRepository:

public IEnumerable<County> GetCounties(string stateAbbr)
    {
        using (var db = new AppDbContext())
        {
            State state = (from s in db.States
                         where s.Abbr == stateAbbr
                         select s).First();

            return context.Counties.Where(c => c.StateID == state.StateID).ToList();
        }
    }

The AppDbContext I created above would go to a custom Initializer:

  public class AppDbContextInitializer : DropCreateDatabaseIfModelChanges<AppDbContext> 
{
    protected override void Seed(AppDbContext context)
    {
        StatesList states = new StatesList();
        context.States.AddRange(states);
        context.Counties.AddRange(new CountiesList(states));

        context.SaveChanges();
    }
}

The problem was, when I executed the code the AppDbContext would load the State and County information correctly in the Initializer, but when it came back into the County Repository, the AppDbContext was empty and would error due to "State has no parameterless constructor". I didn't want my State object to have a parameterless constructor so I looked all day for a solution to why the AppDbContext woulding load in the County Repository. I finally found the following solution:

Exception when loading related objects. Entity Framework

It was a simple solution. Add the parameterless constructor and mark it Obsolete. I did this and it worked perfectly.

My question is, WHY must I do this? I went through multiple examples of CodeFirst using custom Initializer and none of them mentioned requiring an empty constructor or marking it Obsolete.

Is there a better solution or at least an explanation so I can go forward with knowledge instead of confusion?

like image 210
Travis B Avatar asked Jul 21 '15 15:07

Travis B


People also ask

Do you need a Parameterless constructor?

It is required so that code that doesn't know anything about parameterised constructors can construct one of your objects based on the convention that a parameterless constructor is available. On deserialization, and object instance is required so the deserialization process will create one using this constructor.

What is a Parameterless constructor?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new . For more information, see Instance Constructors.

Why can struct have Parameterless constructor?

Although the CLR allows it, C# does not allow structs to have a default parameter less constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor.

Is default constructor mandatory in C#?

In case we don't provide any constructor in the class, the compiler will create a default constructor for that particular class. So by default there will be one constructor of the class and it's mandatory.


1 Answers

There must be a parameterless constructor, but it can be internal or private. ref question 3

like image 177
Sreejith Nair Avatar answered Sep 19 '22 19:09

Sreejith Nair