Is it possible to use a constructor with EF entities?
I want to add a new instance of an Entity Framework entity and add it to a List<>
i.e.
List<MyObject> objectList = new List<MyObject>();
objectList.Add(new MyObject( "property" , 1);
instead of
List<MyObject> objectList = new List<MyObject>();
MyObject object = new MyObject();
object.Name = "property1";
object.ID = 1;
objectList.Add(object);
Yes, you can absolutely do that.
Entity Framework Code First is all about persistence ignorance. That is, you can write code the way you always have and the persistence part “just works”. In reality there are a few limitations on that goal, but for the most part it works as advertised.
Note that there must be a parameterless constructor as well (so that Entity Framework can instantiate instances of the object automatically). But you can also have as many parameterized constructors as you want.