Search code examples
c#sqlframeworksentitycrud

Insert object EF not working


i want insert object to sql server data base, i using EF 6.x. But when i execute Create method, this success and not show any error but in a database is empty :/.

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

Solution

  • You need to call SaveChanges

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