【问题标题】:Insert object EF not working插入对象 EF 不起作用
【发布时间】:2018-01-10 17:59:10
【问题描述】:

我想将对象插入到 sql server 数据库中,我使用的是 EF 6.x。但是当我执行 Create 方法时,这个成功并且没有显示任何错误但在数据库中是空的:/。

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

【问题讨论】:

    标签: c# sql frameworks entity crud


    【解决方案1】:

    您需要致电SaveChanges

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

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 2014-04-12
      相关资源
      最近更新 更多