2017-08-03 74 views
0

我想插入對象到sql server數據庫,我使用EF 6.x.但是,當我執行創建方法,這個成功,並沒有顯示任何錯誤,但在數據庫中是空的:/。插入對象EF不工作

public class ProductoEntity 
{ 
     public int ID { get; set; } 
     public string Descripcion { get; set; } 
     public string Categoria { get; set; } 
     public int Precio { get; set; } 


     public Producto toSql() 
     { 
      var sqlEntity = new Producto(); 
      sqlEntity.Descripcion = this.Descripcion; 
      sqlEntity.Categoria = this.Categoria; 
      sqlEntity.Precio = this.Precio; 

      return sqlEntity; 
     } 

     public bool Create() 
     { 
      bool result = false; 

      try 
      { 
       StoreEntities context = new StoreEntities(); 
       context.Producto.Add(toSql()); ; 
       result = true; 
      } 
      catch (Exception ex) 
      { 
       throw new Exception(ex.Message); 
      } 

      return result; 
     } 
} 

回答

6

你需要調用SaveChanges

StoreEntities context = new StoreEntities(); 
context.Producto.Add(toSql()); 
context.SaveChanges();