none
How to programmatically do this insert with EF 6? RRS feed

  • Question

  • I want to programmatically insert this record into the database using EF6, but cant' get it to work.  My EF stuff is in a class library project and I'm trying to do this insert as a test/proof of concept to be sure I have it set up correctly when the DAL is in its own project.  The front end for now is a simple console app.  I believe I have all connection info and references correct in the Console Application.  I think the issue is me not understanding how to instantiate this correctly.

    My edmx file is named SyncModel.edmx and has a Backstop_Contacts class with basic contact properties is it. I believe the database context class is BackstopEntities becasue this is in the SyncModel.Context.cs file and inherits from the DbContext class.

    This is what I've tried, but I don't see things come into context as expected.  For example, I was hoping to see an add() method, but didn't.  Hopefully, I haven't mixed up the context and Model.  Nothing shows up in intellisense after Backstop_Contacts which is where I get lost.

     using (var myEntity = new BackstopEntities())
                {
                    var myContact = new Backstop_Contacts { firstname = "Elvis", lastname = "Presley" };
                    
                   myEntity.Backstop_Contacts.........

    This is the solution in case I referenced something incorrectly in my post:



    Thursday, September 25, 2014 7:25 PM

Answers

  • Hello,

    Please check the reference list of your console application to see if you have add a reference to the EntityFramework.dll, if not, please add a reference to the dll from your DAL project and also copy the connectionStrings section as below from the app.config file of your DAL project to your console application app.config file:

    <connectionStrings>
    
        <add name="DFDBEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\Projects;initial catalog=DFDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    
      </connectionStrings>
    

    When using a schema similar with yours, we also need to install the Entity Framework to the final project.

    If this does not work for you, please let me know.

    Bets Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 26, 2014 3:38 AM
    Moderator