简体   繁体   English

实体框架未将更改保存到本地数据库

[英]Entity Framework not saving changes to a Local DB

I'm new to C# and Entity Framework and I'm trying to write an application to improve my knowledge. 我是C#和Entity Framework的新手,我正在尝试编写一个应用程序以提高我的知识。

I think I've set up everything correctly and I don't get any type of error. 我认为我已经正确设置了所有内容,并且没有收到任何类型的错误。 The problem is that I can not add a new record to localDb in Visual Studio 2017. 问题是我无法在Visual Studio 2017中将新记录添加到localDb。

Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

Here is my code 这是我的代码

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();
}

and here my app.config : 这是我的app.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>

I think there is some mistake in the app config file but the connection string was generated by entity framework. 我认为应用程序配置文件中存在一些错误,但是连接字符串是由实体框架生成的。 So I'm stuck MyDB Structure 所以我卡住了MyDB结构

Because your localdb is copied to bin folder every time you run it. 因为您的localdb每次运行都会复制到bin文件夹中。 It gets overwritten everytime you run/debug it. 每次您运行/调试时,它都会被覆盖。 Main file in the project folder is not updated. 项目文件夹中的主文件未更新。 For detailed answer: https://social.msdn.microsoft.com/Forums/en-US/393bddc6-0b85-4c27-9475-27172e50d2d9/entity-framework-doesnt-save-new-record-into-database?forum=adodotnetentityframework 有关详细答案: https : //social.msdn.microsoft.com/Forums/zh-CN/393bddc6-0b85-4c27-9475-27172e50d2d9/entity-framework-doesnt-save-new-record-into-database?forum= adonetnetityframework

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM