33

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?

2
  • if your state class has a custom constructor, then you must also provide another constructor (which does not take parameters). why? rules of .net. Commented Nov 23, 2016 at 1:44
  • 1
    @BKSpurgeon Well, that is not entirely correct. If you don't have a constructor defined, then the implicit default constructor (parameterless) exists. If you specify a constructor, and want to use both parameterized and parameterless, then you have to specify it explicitly. Commented May 11, 2017 at 21:04

1 Answer 1

49

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

Sign up to request clarification or add additional context in comments.

3 Comments

I started getting exceptions once I changed my parameterless constructor to private. It looks like after the change all the complex types in that particular model are coming back null.
This constructor is called via reflection from entity framework
Ah, I really hate to upvote, because it's got exactly 42 votes now. This is a GREAT answer and deserves this number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.