Where to place my DbSet in my project with Entity Framework?


Well this may be a silly question but I would like to know how other people who use this framework work. I am new using these technologies, I am using Visual Studio with ASP.NET MVC 5 and Entity Framework and I have just started a new project, I am with the creation of the database by code first, I have for example two models Father and Son


    public class Padre
        public int ID { get; set; }
        public string Nombre { get; set; }
        public int Edad { get; set; }

    public class PruebaDBContext : DbContext

        public System.Data.Entity.DbSet<Pruebas.Models.Padre> Padres { get; set; }

        public System.Data.Entity.DbSet<Pruebas.Models.Hijo> Hijos { get; set; }


I have the model of Hijo.cs , my questions would be:

1- It would be nice to put my DbSets in a class of the model (As I have it in the example) or you should put it all together in another specific class to create the tables (In a separate file).

2-Can I place the DbSets anywhere other than in my TestDBContext class without having to use DbContext again but not create another database?

asked by Jason0495 08.07.2018 в 06:18

1 answer


By order in your classes I recommend you create your context (with your dbSet) in a separate class so that it is scalable and that the parent entity or entity is separated and ordered in your project. With regard to the database, there is no mess, you will only create the one or the contexts that you define.

It is important to clarify that if in your class you already have a using like this:

    using System.Data.Entity;

It would not be necessary for your dbset to go that long:


I hope it serves you!

answered by 09.07.2018 / 20:55