Is it really necessary to create a property that indicates the Foreign-key of a One-To-Many relation?

Asked

Viewed 87 times

1

I would like to know if the information I found after searches, which briefly, is in the link below, proceeds:

http://www.tiselvagem.com.br/desenvolvimento/chave-estrangeira-e-associacao-independente-no-entity-framework/

Question:

In a hypothetical situation, if we have the class Car and the class Brand, where Car has a Brand and Brand may have several Cars (One-to-Many), the Entityframework forces me to create a 'Tag' field in the object Car, or else I would have to carry the whole object Brand at the time of an inclusion/amendment of a Car if I don’t want to use a field Marcaid ?

Reason for the question:

Even in the links where I found this information, they say that this practice hurts OO’s good practices. There is a better solution for these cases?

1 answer

1

Entityframework requires me to create a field MarcaId in the object Car?

Yes. It’s the default framework.

Additionally, you would have to add a browsing property (as below) to stay entirely within the standard:

public virtual Marca Marca { get; set; }

I would have to carry the whole object Marca at the time of an inclusion/amendment of a Carro?

No. The cargo of Marca is lazy by default. It’s done on the navigation property I mentioned above.

I do not agree that the Entity Framework standard hurts OO practices.

...if I don’t want to use a field MarcaId?

If you don’t want to use it, it won’t work simply because you’re not following the Framework standard. MarcaId is bank mapped, so it needs to exist as a property in the class Carro.

About the tutorial

It has several inaccuracies. I do not recommend relying on it.

  • I edited the excerpt on the question i would have to load the entire Mark object at the time of an inclusion/change of a Car? , I didn’t specify the situation.

  • @Joaquimmagalhães I edited the answer.

  • Personally I find it very strange! If I have already configured the key of the Tag object, why does EF force me to create a field MarcaId instead of using Marca.Id. I am changing the model (a representation of a real world concept) for the purpose of meeting the persistence framework. Thank you for the sloppiness and forgive me for the insistence... rs

  • @Joaquimmagalhães Because a Model reflects the database data representation. If for example you have a DropDownList on an editing screen and it is based on MarcaId (assuming, hypothetically, that you can change the brand of the car), it makes no sense to use Marca.Id. You’d be altering a data of Carro, and not of Marca.

  • I understand. It’s more related to a Model than a Domain of the problem. In this view it makes sense!

  • MVC with Entity Framework is not strict DDD. It follows some things from the DDD, so much so that we can say that it implements the DDD, but in a more flexible way.

Show 1 more comment

Browser other questions tagged

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