none
How To Organize Entity Files In A Project RRS feed

  • Question

  • I'm using Entity Framework to connect to our databases and the project is getting clutter with multiple edmx files. I tried to drag them to a Data Models folder that I created, but that broke the project. Is there a way to put the EF model files into a separate folder of their own?

    MCSD .NET developer in Dallas, Texas

    Wednesday, March 18, 2015 3:14 PM

Answers

  • If using DB first and you have a DAL classlib project as an example, you can make a Folder/a namespace called Model1, Model2 Model3 etc etc or or whatever you want to call the Folder/namespace in the project, and you point EF to each individual  folder/namespce to create an EF model in the folder/namespace. I have done that for model seperation within the same project using DB first. But this was at EF model creation time.
    • Marked as answer by DallasSteve Friday, March 20, 2015 9:04 PM
    Thursday, March 19, 2015 2:24 PM

All replies

  • Hello DallasSteve,

    >> Is there a way to put the EF model files into a separate folder of their own?

    For database first and model first approaches, as far as I know, we cannot specify the output directory, EF is in charge of where it puts stuff and where it expects them to be. For your case, my suggestion is that you could place every edmx file to a separate folder, if your edmx files are already your project, you could create correspondent folders(e.g. you have edmx files: Model1 and Model2, then you could create two folders named Model1 and Model2) and move these edmx files to these folders, so that each edmx would have itself entity files even there are same name files in other edmx models.

    Another approach is to specify these entity files to output to a separate project, for details, you could check this article.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 19, 2015 2:21 AM
    Moderator
  • If using DB first and you have a DAL classlib project as an example, you can make a Folder/a namespace called Model1, Model2 Model3 etc etc or or whatever you want to call the Folder/namespace in the project, and you point EF to each individual  folder/namespce to create an EF model in the folder/namespace. I have done that for model seperation within the same project using DB first. But this was at EF model creation time.
    • Marked as answer by DallasSteve Friday, March 20, 2015 9:04 PM
    Thursday, March 19, 2015 2:24 PM
  • darnold

    Thanks, that worked.  That's exactly what I wanted.  The namespace was the key to getting it to work.


    MCSD .NET developer in Dallas, Texas


    Friday, March 20, 2015 9:04 PM
  • I did it like this:

    • I copied the .cs file (eg. DAL\Component.cs) on disk to the folder  I wanted to belong to (eg: Model\Component.cs)
    • Looked up the same the class  in the .csproj file (with notepad) and replaced the namespace at <Compile Include> tag, eg: <Compile Include="Dal\Component.cs" /> to <Compile Include="Model\Component.cs" />
    • Added Component.cs to project's Model folder in Visual Studio (show all files on the top of Solution explorer, right click on the class  => Include in Project)
    • Deleted the original class from DAL folder(right click delete)

    Of course the namespace in the class must be renamed too.



    and replaced the old namespace (eg. "DAL") to the new one (eg. "Model"), here's an example

    Thursday, February 20, 2020 1:40 PM
  • I did it like this:

    • I copied the .cs file (eg. DAL\Component.cs) on disk to the folder  I wanted to belong to (eg: Model\Component.cs)
    • Looked up the same the class  in the .csproj file (with notepad) and replaced the namespace at <Compile Include> tag, eg: <Compile Include="Dal\Component.cs" /> to <Compile Include="Model\Component.cs" />
    • Added Component.cs to project's Model folder in Visual Studio (show all files on the top of Solution explorer, right click on the class  => Include in Project)
    • Deleted the original class from DAL folder(right click delete)

    Of course the namespace in the class must be renamed too.



    and replaced the old namespace (eg. "DAL") to the new one (eg. "Model"), here's an example

    If using EF Core there are tools available to scaffold to the proper folders or this can be done on the PowerShell command line.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, February 24, 2020 7:44 PM