none
What references do I need in a main project calling another project that utilizes EF? RRS feed

  • Question

  • I'm new to EF.  When starting a new project that will use EF I seen people put the entity framework code into a class library, so that it could easily be used with other solutions.  This seems like a great idea to me, so I want to do that. Right now I'm working on a console app for quick testing of ideas, so I've got a console project in my solution, and have added a library project to the solution, which I'll use to put all of the entity framework logic.

    What I want to know is what references do I have to put into my console app?  A reference to the class library, obviously, but do I also have to put a reference to System.Data.Entity namespace in the console app?  Do I have to put a connection string to the SQL Express database I'll be using that EF will be running against, into the console's app.config file?


    Rod
    Tuesday, January 31, 2012 6:28 PM

Answers

  • Hi Rod;

    Like everything else that all depends. If the library code is set up to create the ObjectContext / DbContext and all the querying will be initiated and executed from within the Library project and the results are passed back to the console app via DTO, data transfer object, then only reference needed in the console app is a reference to the Library project. But if the console app will be creating the ObjectContext / DbContext and doing some querying then not only will you need a reference to the Library project but also a reference to System.Data.Entity.dll as well as a copy of the connection string used for the Entity Framework. You can copy and past the App.Config file from the Library project to the console project and edit it so that the only thing in the config file for the console app is the configuration and connectionString nodes as shown below :

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="TheEntitiesNameEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string= ... ;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
    </configuration>
    

    You will also need a using statement to the Library code. That should be all you need.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Rod at Work Tuesday, January 31, 2012 8:14 PM
    Tuesday, January 31, 2012 7:08 PM

All replies

  • Hi Rod;

    Like everything else that all depends. If the library code is set up to create the ObjectContext / DbContext and all the querying will be initiated and executed from within the Library project and the results are passed back to the console app via DTO, data transfer object, then only reference needed in the console app is a reference to the Library project. But if the console app will be creating the ObjectContext / DbContext and doing some querying then not only will you need a reference to the Library project but also a reference to System.Data.Entity.dll as well as a copy of the connection string used for the Entity Framework. You can copy and past the App.Config file from the Library project to the console project and edit it so that the only thing in the config file for the console app is the configuration and connectionString nodes as shown below :

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="TheEntitiesNameEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string= ... ;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
    </configuration>
    

    You will also need a using statement to the Library code. That should be all you need.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Rod at Work Tuesday, January 31, 2012 8:14 PM
    Tuesday, January 31, 2012 7:08 PM
  • Thank you, Fernando for your reply.  I've got a follow-up question I'd like to ask.  When you said, "If the library code is set up to create the ObjectContext / DbContext and all of the querying will be initiated and executed from within the Library project and the results passed back to the console app ..."  I am taking you to mean something like this:

    using (AWLTEntities context = new AWLTEntities())
    {
    	var customer = context.Customers.Where(c => c.LastName == "Smith").First();
    	
    	//do some other stuff, modify some data, etc
    
    	context.SaveChanges();	//this saves changes back to the database
    }
    
    

    Am I correct?


    Rod
    Tuesday, January 31, 2012 7:42 PM
  • Hi Rod;

    I believe the answer to your question is yes, but to clarify my last post :

    If the code you posted or similar code like it only show up within the Library project then all you need in the console project is a reference to the Library project. But if that code shows up in the console project then you will need to add all the things I stated in my first post.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, January 31, 2012 8:01 PM
  • Thank you very much, Fernando, for clarifying what you were saying.
    Rod
    Tuesday, January 31, 2012 8:14 PM
  •  

    Not a problem Rod, always glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, January 31, 2012 8:21 PM