Entity Framework Properties Interface Type

I am creating an application using the first entity code, and am facing some problems with EF restrictions, following the principle of interface segration. Part of application model architecture in UML

public interface IProduct { int Id { get; set; } ICollection<IProcess> Processes { get; set; } ICollection<ILine> Lines { get; set; } String Description { get; set; } String Number { get; set; } String Name { get; set; } } 

The problem is that the Processes and Lines property leads to the fact that it cannot determine which class is in a particular type (I suppose).

I know that I could achieve almost the same thing using abstract classes. The reason why I didn’t just do this is because I am wrong to change the model due to EF limitations.

What would be the best way to solve this problem? Any alternative to EF that allows interfaces as a navigation property.

+4
source share
2 answers

There is only one way to solve this problem - use specific classes that are stored in the database. EF does not support another way. Even using abstract classes will not help you unless you draw the entire inheritance tree (do not do this).

If you want a property to display an interface, you must offer a second property without a mapping, which will perform the conversion from a property that displays a specific type inside.

Simply, if you want to use EF, you have to bend your architecture, following its set of functions.

+3
source

I know this is an old thread, but in case anyone else comes across it, I have a slightly better solution for using EF.

Basically, I have two interfaces

 public interface IProduct { int Id { get; set; } String Description { get; set; } String Number { get; set; } String Name { get; set; } } 

and

 public interface IEFProduct { ICollection<IProcess> Processes { get; set; } ICollection<ILine> Lines { get; set; } } 

I keep the IProduct interface in my contract project, so it is still completely independent of the rest of my model, but I put the IEFProduct interface in my Model project, as this contains a specific implementation. This means that I cannot access the processes and lines from everything that implements the interface, not a specific type, but in my case it caused a problem.

Another way forward will be to use DTO.

All your models will use the interface, but your actual EF implementation will use Concrete DTO, in a data layer that you would display between them either manually or using Automapper - of course, this can have a small performance impact.

0
source

Source: https://habr.com/ru/post/1392002/


All Articles