Entity Framework and parameterized constructor

Asked

Viewed 222 times

3

When I create domains, I usually create a parameterized constructor for the same:

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 ( ) { }
    }
}

This way the user of the class has the option to pass the value of the properties at the same moment of the instantiation of the object. Or pass them later through the setters.

This is valid in the RU?

  • 3

    I don’t think there’s any need, since you can do it var unity = new Unity{Abreviation = "un", Description = "unity"};. But how so if it is valid in EF? Could explain better?

  • It is valid yes ... !!! This goes for every developer, or development group, or rule followed. But this is no problem

  • 1

    I would say it’s something useless. I can do it like this: var unity = new Unity { Abbreviation = abbreviation, Description = description };

  • @Ciganomorrisonmendez because useless, there is development with DDD that this is more than valid ... because useless ?

  • 3

    This may help you or perhaps answer: http://answall.com/q/73530/101 EF does not have a specific need for this. The second empty constructor makes the first unnecessary, as already reported by Randrade. If only the first would be different.

  • 3

    @John DDD. It’s explained.

  • @Randrade I mean, if I pass the values of the properties through the builder, give an Add and have saved? Will the EF save normally? I did not remember this syntax of C#, as I came from other languages that do not have the same resource, I ended up bringing together some habits.

  • I did not go deeper because it is a subject that already has answers on the site.

Show 3 more comments

2 answers

4


If I pass the values of the properties through the constructor, give an Add and have them saved? EF will save normally?

Yes, he will save normally.

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

This question can help you understand a little more.

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

I drew up this simple example for you to see working what was said.

  • I am sorry to return to that question, but another question has arisen concerning the subject. Suppose in a domain I create a parameterized constructor to ensure that some class properties are initialized. Does EF have the intelligence to perceive the constructor with parameters and use it when creating objects? That is, the EF must instantiate the object by passing the parameters to the constructor var anyObject = new AnyObject(par1, par2, ...) and no longer by the empty default constructor. EF has this intelligence to realize that there is a constructor with parameters? I don’t know if I was clear on the question.

  • @Matheussaraiva I think you’re having a confusion about what the Entity Framework is and what it is C#. That’s what you’re talking about C#, the EF enters only when performing some operation in the database (in your case). If the object has the values, the EF will save those values.

  • But when I do a query for example, EF does not return results in the form of domains. For example, if I ask the client whose ID is 250, how does he return it to me? Wouldn’t it be in the form of a Client object with the values defined in the properties and if more than one result would return a collection of Clients? For example: var pedidos = from p in contexto.Orders select p; pedidos.ToList(); What returns the method ToList() ?

  • @Matheussaraiva Open another question on the subject, you are making a huge mess. Parameters when instantiating an object is a thing, the return of the EPH is another. One is from C# and the other is from EPH, respectively. Other, the method ToList() will return you a list, if you want only one object, use the First() or the FirstOrDefault(), depending on the case.

-1

  • 1

    This is incorrect. The procedure of Seed does not demand private builders.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.