Entity Framework not saving changes to a Local DB

c# entity-framework-6 winforms

Question

I'm attempting to create an application to expand my understanding of C# and Entity Framework.

I believe I have everything set up properly since there are no errors. My inability to add a new record to localDb in Visual Studio 2017 is the issue.

Any thoughts? I appreciate it.

The code is below.

private void Save_btn_Click(object sender, EventArgs e)
{
        var context = new Database1Entities();

        var aMemberdef = new Members()
        {
            FirstName = Name_txt.Text,
            LastName = LastName_txt.Text
        };

        context.Members.Add(aMemberdef);
        context.SaveChanges();
}

here's myapp.config :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" 
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                 requirePermission="false" />
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <connectionStrings>
        <add name="Database1Entities" 
             connectionString="metadata=res://*/EF.Model1.csdl|res://*/EF.Model1.ssdl|res://*/EF.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB; attachdbfilename=|DataDirectory|\Data\Database1.mdf; initial catalog=Database1; integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
             providerName="System.Data.EntityClient" />
    </connectionStrings>
    <entityFramework>
        <defaultConnectionFactory  
             type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
            <provider invariantName="System.Data.SqlClient" 
                      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
</configuration>

Although I believe the app configuration file contains a mistake, entity framework created the connection string. I'm therefore trapped at MyDB Architecture

1
0
4/20/2017 10:31:42 AM

Popular Answer

because each time you run localdb, a copy is made in the bin folder. Every every time you run or debug it, it is overwritten. The project folder's main file has not been changed. Answer in full: https://social.msdn.microsoft.com/Forums/en-US/393bddc6-0b85-4c27-9475-27172e50d2d9/entity-framework-doesnt-save-new-record-into-database?forum=adodotnetentityframework

0
8/12/2018 6:10:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow