Why in, C# ASP.NET MVC Code First project, POCO Classes must have getters and setters ( {get; set; } ) in order to work?

public float data { get; set; }

or in db context class

public DbSet<SGD.Models.MyData> MyData{ get; set; }

I just don't understand why a simple attribute (without getters and setters) doesn't works.

  • 4
    I'm 99% sure that's entity framework that requires that, not ASP.net MVC. – whatsisname Jul 25 '16 at 16:07
  • 1
    Because they are properties rather than fields. Which means they're internally pairs of methods. That's much easier for a framework to deal with than fields. – Magus Jul 25 '16 at 16:08
  • Apparently the EF authors decided to only support properties and not fields. – CodesInChaos Jul 25 '16 at 16:36

Because Microsoft decided to only support properties in Entity Framework.

There are many reasons why properties are preferred over public fields, but I'll just mention the one that I think is most relevant here: changing from public fields to properties (if the need arises) is a binary breaking change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.