1

How can I share my DBcontext in my web.config without creating multiple connections?

WEB.config:
    <configuration>
      <connectionStrings>
        <add name="daC_Companies" connectionString="Data Source=10.0.2.100;Initial Catalog=XXXXX;User ID=XXXXXXXXXx;Password=XXXXXXXX;Persist Security Info=False" providerName="System.Data.SqlClient" />
      </connectionStrings>


Data Access:
        public class daC_Companies : DbContext
        {
            public DbSet<ClassLibrary.Companies.C_Companies> dbsetC_Companies { get; set; }
        }

And then I have a class called C_Companies referenced above. It works fine but I don't want to have a new DBContext for every class I want to access.

1 Answer 1

1

The DbContext should be specific to the database, not to the object. To add a reference to more db tables, add them as properties into the daC_Companies object:

    public class daC_Companies : DbContext
    {
        public DbSet<ClassLibrary.Companies.C_Companies> dbsetC_Companies { get; set; }
        public DbSet<ClassLibrary.Companies.Object2> Object2s { get; set; }
        public DbSet<ClassLibrary.Companies.Object3> Object3s { get; set; }
        public DbSet<ClassLibrary.Companies.Object4> Object4s { get; set; }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.