SaveChanges()操作仅更新“内存”值

时间:2013-02-06 12:39:29

标签: entity-framework linq-to-entities updates

我使用简单的Console应用程序和数据库并生成.edmx文件。当我插入新行和跟踪更新时,我发现一切正常。但是当我去查看我的数据库的内容时,我没有看到任何更改。看起来我更新了我的数据库的“内存实例”。我应该更改我的ConnectionString OR / AND .edmx文件以更新真实数据库?<登记/> 更新

    <connectionStrings>
            <add name="ProductsEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\Products.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>


    static void Main(string[] args)
    {
         var context = new ProductsEntities(ConfigurationManager.ConnectionStrings["ProductsEntities"].ConnectionString);
         var productForInsert = new Product {CompanyName = "Insert Name", Price = new decimal(22), Description = "dd", Name = "ddd"};
         context.Products.AddObject(productForInsert);
         context.SaveChanges();
         // return only new row as expected ([database rows].CompanyName == "Toshiba")
         // tracking database rows returns expected result too
         var result = context.Products.Where(p => p.CompanyName != "Toshiba");
         foreach (var b in result)
         {
            Console.WriteLine(b.CompanyName);
         }
    }

0 个答案:

没有答案