实体框架C#表在使用add方法时未使用数据更新

实体框架C#表在使用add方法时未使用数据更新,c#,entity-framework,C#,Entity Framework,我已经完成了本教程 youtube.com/watch?v=K4x6eoG7hwY&ab_channel=SeeSharpCode 但仍然无法将数据添加到我的表中。 我没有对这个EF代码做任何手动更改 我确实喜欢教程,但仍然无法向表中添加数据 这就是我写的所有代码: 我的桌子: CREATE TABLE [dbo].[TableTest]( [Id] INT NOT NULL PRIMARY KEY IDENTITY, [Name] NCHAR(50) NOT NULL )

我已经完成了本教程

youtube.com/watch?v=K4x6eoG7hwY&ab_channel=SeeSharpCode

但仍然无法将数据添加到我的表中。 我没有对这个EF代码做任何手动更改 我确实喜欢教程,但仍然无法向表中添加数据

这就是我写的所有代码:

我的桌子:

CREATE TABLE [dbo].[TableTest](
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [Name] NCHAR(50) NOT NULL
)
我的代码:

using (Database1Entities database1Entities = new Database1Entities())            {
                TableTest t = new TableTest
                {
                    Name = "name1"
                };
                database1Entities.TableTest.Add(t);
                int c = database1Entities.TableTest.Count();
                database1Entities.SaveChanges();
            }
在下一行,c变量的值为0。。。如果这有助于理解问题

int c = database1Entities.TableTest.Count();
添加
TableTestModel.Context.cs
class

public partial class Database1Entities : DbContext
    {
        public Database1Entities()
            : base("name=Database1Entities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<TableTest> TableTest { get; set; }
    }
public分部类数据库实体:DbContext
{
公共数据库1实体()
:base(“名称=数据库1实体”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共虚拟DbSet TableTest{get;set;}
}
此外,我正在使用

此应用程序配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  <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.7.2" />
  </startup>
  <connectionStrings>
    <add name="Database1Entities" connectionString="metadata=res://*/TableTestModel.csdl|res://*/TableTestModel.ssdl|res://*/TableTestModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.7.0" newVersion="3.1.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.7.0" newVersion="3.1.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.7.0" newVersion="3.1.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Caching.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.7.0" newVersion="3.1.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Options" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.7.0" newVersion="3.1.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.7.0" newVersion="3.1.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.7.0" newVersion="3.1.7.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>


使用名为Database1.mdf的本地SQL数据库时,当您询问DB实体计数时,您尚未完成事务,请更改为该数据库并告诉我它是否有效:

using (Database1Entities database1Entities = new Database1Entities())            {
                TableTest t = new TableTest
                {
                    Name = "name1"
                };
                database1Entities.TableTest.Add(t);
                database1Entities.SaveChanges(); <----- HERE
                int c = database1Entities.TableTest.Count();
            }
使用(Database1Entities Database1Entities=new Database1Entities()){
TableTest t=新TableTest
{
Name=“name1”
};
数据库1实体.TableTest.Add(t);

database1Entities.SaveChanges();您的计数是正确的..但它仍然不会将新数据添加到数据库中的我的表中..使用'database1Entities.add(t);'而不使用实体引用,如果它不起作用,我可以看到您的dbcontext吗?(记住隐藏连接字符串)我已将其添加到我的主要问题中\n是的,我手动向其添加一个新数据,并将其链接到图片中。啊,好的,连接字符串正在使用EDMX设计器。可能发生的情况是,您正在查看创建的设计器数据库实例,但应用程序在运行时可能正在创建/链接到数据库实例(Database1.mdf)位于不同的数据目录中。(例如,可能位于解决方案目录的/bin/debug/directory?)中,搜索Database1.mdf以查看应用程序是否创建了另一个副本。我使用EDMXs已经很久了,选择仅手动创建DB,并使用表的映射将EF指向它。