1

I following this answer

My auto generated file has this code for dbContext:

public partial class TrafficEngineEntities : DbContext
{
    public TrafficEngineEntities()
        : base("name=TrafficEngineEntities")
    {
    }

I can modify the file to add the additional method with the string parameter:

public partial class TrafficEngineEntities : DbContext
{
    public TrafficEngineEntities()
        : base("name=TrafficEngineEntities")
    {
    }

    public TrafficEngineEntities(string connectionString)
       : base("name=TrafficEngineEntities")
    {
    }

But if I try only write the part to add the additional method in a separated file (to avoid overwrite in case updates), then visual studio said my db tables class aren't part of the dbcontext, like I overwrite everything in the partial class.

public partial class TrafficEngineEntities : DbContext
{
    public TrafficEngineEntities(string connectionString)
       : base("name=TrafficEngineEntities")
    {
    }
}
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118

1 Answers1

3

The problem here is that your manually created TrafficEngineEntities partial class and auto generated TrafficEngineEntities are in different namespaces. So, in fact these are 2 different classes.

Typically, you have namespaces in your solution in align with your solution folders. Auto generated files may not respect this convention or you may accidentally create your manually created partial class in the different folder.

Make your manual class namespace same as in auto generated class and it should solve the problem.

You may suffix your files like TrafficEngineEntities.AutoGenerated.cs and TrafficEngineEntities.Patrial.cs so, you can clear see the difference between auto generated and manually edited files.

Sergey L
  • 1,402
  • 1
  • 9
  • 11