Circular dependency error when EF modelling RRS feed

  • Question

  • Hi everyone, We are developing a new framework for existing database and project. Our business run on classic old .net techniques such as sql command objects or data table etc. But we must move this project to new technology such as MVC presentation, entity framework (code first for existing db). We have done separate in modules(different class library) due to big database(+1000 table and views or stored procedures..).Project leader have decided to do every process in business logic (managers / controllers) belonging to database layer(not sp or functions..) Please give me an advice about development process after my examples about our process.(we can not do some important things.!)

    For example we have a personnel management system (personnel entity, department entity etc. in human resources class library/business layer) and have a different class library / business layer called as expenditure management system(expenditure entity, expenditure category entity). The people have some expenditure detail in database(relationship between entities) so we have must a personnel property in expenditure model like follows;

    Personnel (this is personnel instance for sub - table  / navigation property)

    and in personnel model we have must expanditure Collection for follow the navigation like follows;

    Name Surname etc.
    ICollection<Expenditure> Expenditures

    but this two entities are in different libraries. When I got the human resources library in to the expenditure library, we can do this navigation property, but in expenditure library, we also must have personnel entity instance(for make relationship between entities). We can not do this scenario because of circular dependency in between 2 libraries. I tried interface method so created different library for model interface but EF does not allow relationship based on interface. How can I do this scenario?

    Wednesday, May 20, 2015 7:03 AM

All replies

  • Can you confirm that the picture below represents approximately the structure of your data tables? You have circular references going on as evident in the picture below.

    multi-valued column

    Danny Rosales .NET Developer

    Wednesday, May 20, 2015 8:32 PM
  • My advice to you is don't get caught with using EF directly in the controllers, use N-tier, use DTO(s) and leave the EF entites at the DAL.  


    You can use the repository pattern in the above link.

    I perfer to use the DAO pattern myself.


    The DTO(s) can be used in the viewmodel with data annotations applied if the you put the DTO(s) in a project called Entities and all projects have refernce to the Entites project. A DTO can consist of any type of data from one or more tables,  and the DTO can be a parent object to  children DTO(s) within the parent, like a List<T> within the parent DTO..

    You would need to use something like Automapper to map EF entiters to DTO(s) and vice versa.


    You may want to look at Castle Wndsor IoC dependency injection it will save uou a lot of grief in using the objects


    Again, my advice to you is don't get caught in the trap of using EF in the controllers,  implemnet Seperation of Concerns. and leave the EF entites at the DAL.


    That's where there are tools like this. However, code first may prevent you from using the tool,  and you have got to roll your own DTO(s).


    Wednesday, May 20, 2015 8:47 PM
  • Exactly yes!. HumanResources is a module, also Expenditure is different module. Also we have an "run on database". In this scenario, we have to set a relationship (navigation properties). But circular dependency is a big problem. We have thought a different solution, but in this solution we will have to create same models each modules like accounting, stock etc.

    Human resource will have only base person class, accounting will have same person class with extra accounting details. Stock will have same person class with extra stock details. I will give an example again.

    Human Resources.dll has

    Person Class {Id, Name, Department, BirthDate etc.}

    Accounting.dll has

    Expanditure class {Id, Category, Date, PaymentMethod, PersonelId, Personel(navigation / proxy)}

    Again Person Class{Id, Name, Department, BirthDate, IColection<Expanditure> ExpenditureList}


    will have stock classes and Person class. But in this point I think to seperate/do create again Person per modules. So, in Accounting Person will have only accounting detail, in Stock Person will have only stock detail not another details..

    We researched entity framework inheritance TPH etc. But we have no discriminator columns in live database(has +1000 table/view also +5000 sp !) So we cannot implement inheritance techniques... we can only do copy models for navigation/subdetail.. this is good solution according to you?(I don't think so:))

    Wednesday, May 20, 2015 9:56 PM
  • We researched entity framework inheritance TPH etc. But we have no discriminator columns in live database(has +1000 table/view also +5000 sp !) So we cannot implement inheritance techniques... we can only do copy models for navigation/subdetail.. this is good solution according to you?(I don't think so:))

    It's your show, and you are the one that has to pay the price in whatever direction you take. You are not copying models, and you are copying objects. You make it too complicated using any ORM, and you will pay the price at some point. Again,  I suggest that you don't fall into the fool's trap of using EF directly in the controllers and learn how to use the simplistic DTO.

    Like I said, it's your show, you are the  one that has to dance in it, make the bed and laydown in the bed too.

    Thursday, May 21, 2015 5:58 PM