0

我已将所有代码从原始代码更改ADO.NET SQLCommandEntity Framework,以便将来更容易更改我的代码。但是,我意识到它有很多缺点,Entity Framework它不像将原始SQL命令注入数据库那么简单。此外,我曾经Reverse Engineering生成过Models & Mappingfor MS Sql Server

目前,我正在尝试执行以下操作,但没有一列得到更新。

string sql = @"UPDATE [ProductDB] SET CreatedProduct_Time=getdate(), CreatedProduct_Date=getdate()" + " WHERE [Material_No] = @Material_No";

db.Database.ExecuteSqlCommand(sql, new SqlParameter("@Material_No", materialnotxt));

列没有得到更新。

我怀疑是否Entity Framework会帮助我维护我的代码以供将来使用,使用它而不是旧的是否值得头疼raw SQL code?到目前为止,存在许多限制,并且需要更高的学习曲线。

context.Database.ExecuteSqlCommand我在网上找到的一些令人困惑的部分和这个MSDN http://msdn.microsoft.com/en-us/library/bb738684.aspx有什么区别,代码看起来与我的方法完全不同。

编辑

我使用了一种不同的方法来插入Date和,Time同时插入来自textbox.

using (var db = new ROGContext())
        {

            ProductDB product = new ProductDB
            {
                Material_No = long.Parse(MaterialNo_txtbox.Text),
                Product_Line = ProductLineDropDownList1.SelectedItem.Text,
                Product_Description = Description_txtbox.Text,
                Size = Size_txtbox.Text,
                UOM = UOM_txtbox.Text,
                SupplierID = long.Parse(SupplierCountryListBox.SelectedItem.Value),
                CreatedProduct_Date = DateTime.Parse(System.DateTime.Now.ToShortDateString()), //in the SQL database I have set the datatype as date to get yyyy/mm/dd
                CreatedProduct_Time = DateTime.Now.TimeOfDay  //in the SQL database I have set the datatype as time(0) to get hh:mm:ss
            };

            long queryselect = (from materialno in db.ProductDBs
                               where materialno.Material_No == product.Material_No
                               select materialno.Material_No).SingleOrDefault();

            if (queryselect != long.Parse(materialnotxt))
            {
                Label1.Text = "Product successfully added in database";
                Label1.Visible = true;
                db.ProductDBs.Add(product);
                db.SaveChanges();             
            }             
4

1 回答 1

0

您可以Context.ExecuteStoreCommand用于插入、更新和删除。

参见Entity Framework 4.0 Recipes: A Problem-Solution Approach第 64 页

回答您编辑的部分:

  1. ExecuteStoreQuery选择
  2. ExecuteStoreCommand用于插入、更新和删除

Entity Framework v4 – 提示和技巧

于 2013-03-08T19:25:59.627 回答