none
How do you initialize a database with an ObjectContext? RRS feed

  • Question

  • Ok, for TDD reasons I recently converted from using a DbContext in my Repository and UnitOfWork classes, to using an ObjectContext. My question is: How do you initialize a database with an ObjectContext?

    Here is my solution for now -- obviously I had to tweak my IDatabaseInitializer to find my object sets;

    		#region Construction
    
    		public SqlUnitOfWork()
    		{
    			_context = new ObjectContext(SqlUnitOfWork.ConnectionStringName);
    			_context.ContextOptions.LazyLoadingEnabled = true;
    
    			var dbContext = new DbContext(_context, false);
    			Database.SetInitializer<DbContext>(new SbaDatabaseInitializer());
    		}
    
    		#endregion

    Is there a better way?

    Thanks,

    Jack


    Jack

    Tuesday, August 13, 2013 2:59 PM

Answers

  • Hi jlavallet;

    In your original post you stated, "I recently converted from using a DbContext in my Repository and UnitOfWork classes, to using an ObjectContext.", a couple of things first database initialization is not supported using ObjectContext and Code First is only supported by Entity Framework 4.1 and higher using DbContext. So I am a little confused by your last post when you state, "I am not using Model First; I am using Code First.  ".

    To your statement, "I looked into implementing the IDatabaseInitializer interface", as you stated that works with DbContext.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, August 15, 2013 2:36 PM

All replies

  • Hi Jack;

    Please read the following answer by Rowan Miller in the following post. [Click Here]

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, August 13, 2013 4:19 PM
  • Hi Fernando, thanks for the reply.  

    I am not using Model First; I am using Code First.  

    Anyway, I looked into implementing the IDatabaseInitializer interface and passing an ObjectContext in the place of a DbContext -- no luck, the interface requires the generic type to be a DbContext.  

    I decided to go to a testable DbContext based solution based on finding this.

    Thank for trying.

    Jack


    Jack

    Thursday, August 15, 2013 7:28 AM
  • Hi jlavallet;

    In your original post you stated, "I recently converted from using a DbContext in my Repository and UnitOfWork classes, to using an ObjectContext.", a couple of things first database initialization is not supported using ObjectContext and Code First is only supported by Entity Framework 4.1 and higher using DbContext. So I am a little confused by your last post when you state, "I am not using Model First; I am using Code First.  ".

    To your statement, "I looked into implementing the IDatabaseInitializer interface", as you stated that works with DbContext.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, August 15, 2013 2:36 PM