0

At the minute I am creating my entities using unit of work principle that their is only one context per request.

But I now have a request that I must be able to change the database on the fly at runtime this would be done on the loading of application and they have allowed us to reload the application.

public void AddToAppointment(Appointment newAppointment)
{
        using (var myContext = new SMBASchedulerEntities())
        {
            myContext.Appointments.Add(newAppointment);
            myContext.SaveChanges();
        }
}

The above code is in my SourceContext class and looking for some guidance on this.

My main question is thus can I change the SMBASchedulerEntities to get its connection information from a class if so what would this class look like and function as. The databases will all have the same schema.

What is the best practise for this method?

Edit 2

Sorry I should have stated my context class is like this

public class SourceContext : ContextBase
{
    //    public SMBASchedulerEntities _sourceEntities = new SMBASchedulerEntities();
    // private  SystemDa _systemDB = new SystemDa();

    then my other methods
}

Which context class should I pass the connection to and how would I build that?

Edit 3

This is showing my main entity class which is autogenerated.

  using System;
  using System.Data.Entity;
  using System.Data.Entity.Infrastructure;
  using System.Data.Entity.Core.Objects;
  using System.Linq;

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

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          throw new UnintentionalCodeFirstException();
      }
  }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

1 Answers1

2

Use DbContext overload for constructor. It can take connection string to your database.

public void AddToAppointment(Appointment newAppointment, string connectionString)
{
        using (var myContext = new SMBASchedulerEntities(connectionString))
        {
            myContext.Appointments.Add(newAppointment);
            myContext.SaveChanges();
        }
}

Edit

If you look closely, your db-first generated class conviniently marked as partial, so you free to add new file called "MyPartialContext.cs" which will contain this:

public partial class SMBASchedulerEntities
{
    public SMBASchedulerEntities(string connectionString) : base(connectionString) {}
}
eocron
  • 6,885
  • 1
  • 21
  • 50
  • I am trying not to have to modify all fifty calls to to the database is their not a more elgant solutions to this.. – c-sharp-and-swiftui-devni Jan 02 '18 at 02:15
  • Where you found fifty calls? Im clearly see only one call per method call. – eocron Jan 02 '18 at 02:16
  • when i try to place a conneciton string toward my entity it says it does not exist with a parameter as string – c-sharp-and-swiftui-devni Jan 02 '18 at 02:16
  • my class contains fity calls simlar to that above one i was providing that as a example of how i call out to the database. – c-sharp-and-swiftui-devni Jan 02 '18 at 02:17
  • 2
    Then, obviously, you should create it with inhereting base(connectionString) constructor. If you need to create such big changes, I recommend you to perform refactoring, in which your methods take your context as argument. And context itself is passed from upper layer, this way you atleast can reuse method in different connections and even combine them under one transaction. – eocron Jan 02 '18 at 02:17