C#实现物料分类编辑,查询时字段映射不匹配
Base_GoodsType的字段如下:
namespace WarehouseManage.Model { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial; [Table("Base_GoodsType")] public partial class Base_GoodsType { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Base_GoodsType() { Base_Goods = new HashSet<Base_Goods>(); Base_Region = new HashSet<Base_Region>(); } [Key] public int TypeID { get; set; } [StringLength(50)] public string Name { get; set; } public int? State { get; set; } public string UserID { get; set; } public DateTime? LastTime { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Base_Goods> Base_Goods { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Base_Region> Base_Region { get; set; } } }
Base_GoodsTypeDto的字段如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WarehouseManage.Model.Dto { public class Base_GoodsTypeDto { public int Id { get; set; } public int TypeID { get; set; } [DisplayName("类别名称")] public string Name { get; set; } [DisplayName("状态")] public int? State { get; set; } [DisplayName("修改人")] public string UserID { get; set; } [DisplayName("修改时间")] public DateTime? LastTime { get; set; } public virtual ICollection<Base_Goods> Base_Goods { get; set; } public virtual ICollection<Base_Region> Base_Region { get; set; } } }
Base_GoodsTypeDto.service获取单挑记录代码如下
/// <summary> /// 获取单条记录 /// </summary> /// <param name="exp"></param> /// <returns></returns> public Base_GoodsTypeDto Get(Expression<Func<Base_GoodsTypeDto, bool>> exp) { try { using (var db = new WarehouseContext()) { var where = exp.Cast<Base_GoodsTypeDto, Base_GoodsType, bool>(); var info = db.Base_GoodsType.AsNoTracking().FirstOrDefault(where); return Mapper.Map<Base_GoodsType, Base_GoodsTypeDto>(info); } } catch (Exception e) { log.Error("获取单条物料类别记录出错。", e); return null; } }
Cast<Base_GoodsTypeDto, Base_GoodsType, bool>(),在进行映射时需要字段对应,之前dto只有Id字段,没有TypeID字段,导致报错,所以加上了TypeID字段,只在分页查询时才
使用Id字段,(前端框架问题)