插入对象 EF 不起作用
Insert object EF not working
我想将对象插入 sql 服务器数据库,我使用 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;
}
}
您需要致电SaveChanges
StoreEntities context = new StoreEntities();
context.Producto.Add(toSql());
context.SaveChanges();
我想将对象插入 sql 服务器数据库,我使用 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;
}
}
您需要致电SaveChanges
StoreEntities context = new StoreEntities();
context.Producto.Add(toSql());
context.SaveChanges();