locked
Object Context and Connection String named differently RRS feed

  • Question

  • Hello,

    I am using EF4 and when I create the model I want the ObjectContext to have the name "Context":

      public partial class Context : ObjectContext
      {
    
        public Context() : base("name=Context", "Context")
        {
          this.ContextOptions.LazyLoadingEnabled = true;
          OnContextCreated();
        }
    
      
        public Context(string connectionString) : base(connectionString, "Context")
      {
          this.ContextOptions.LazyLoadingEnabled = true;
          OnContextCreated();
        }
    
      
    
        public Context(EntityConnection connection) : base(connection, "Context")
        {
          this.ContextOptions.LazyLoadingEnabled = true;
          OnContextCreated();
        }

    The problem is that I get the connection string names also "Context".

    Can't I name the connection string differently from the context without the need to go in the code?

    It makes sense to me to have ObjectContext named Context and the connection string the name of the database.

    Can I have more then one Context?

    I will implement Unit Of Work.

    Thanks,

    Miguel 

    Thursday, July 8, 2010 12:39 AM

Answers

  • The designer only supports one objectcontext per model. However you can have more then one objectcontext that shares the same entityconnectionstring defined on the app.config. You can have one objectcontext that contains entities related to customer and other object context that contains entities related to orders to keep things more organized if your model is getting big. 

    I don't think there is any way from the designer to control the name of the objectcontext separately from the name of the connectionstring. Both objectcontext name and connectionstring name is derived from EntitiyContainer property in the properties window when you right click anywhere on the designer.

     


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    Thursday, July 8, 2010 1:27 AM