Entity Framework and parameterized constructor

3

When I create domains, I usually create a parameterized constructor for it:

namespace Models
{
    public class Unity
    {
        public string Abreviation { get; set; }
        public string Description { get; set; }

        public Unity (string abreviation, string description)
        {
            Abreviation = abreviation;
            Description = description;
        }

        public Unity ( ) { }
    }
}

In this way the user of the class has the option of passing the value of the properties at the same time as the instantiation of the object. Or pass them later through the setters.

Is this valid in EF?

    
asked by anonymous 27.05.2016 / 22:57

2 answers

4

If I pass the property values through the constructor, give an Add and have it save? Will EF save normally?

Yes, it will save normally.

When creating a parameterized constructor, instantiating by passing the parameters you will be creating an object normally. It is not normal to do this in C# , but it works.

This question might help you understand a little more.

Read also this comment , I think it will help you understand a little more.

I've made this simple example for you to see what works.

    
28.05.2016 / 01:31
-1

The first constructor is valid and recommended in case you are using DDD and want to apply TDD, but in this case the second constructor should be private, just so that it could be used in EF Migrations. Ref: link

    
27.05.2016 / 23:19