c# automapper 总是转换空对象
c# automapper always convert null object
automapper 总是转换 null 对象并且它不会抛出任何异常这里是我的模型和我使用的方法...当我在 ProductModel 模型中初始化 ProductDetailsModel 时我想要什么我也想在 Product 模型中初始化... ProductModel 中的 ProductDetailsModel 和 CategoryModel 这两个对象都没有在 Product
中初始化
public class Product
{
[Key]
public int ID { get; set; }
public int? IDCategory { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public int Quantity { get; set; }
public List<Image> Images { get; set; }
[ForeignKey("IDCategory")]
public Category Category { get; set; }
public virtual ProductDetails ProductDetails { get; set; }
}
public class ProductModel
{
[Key]
public int ID { get; set; }
[Required(ErrorMessage ="حقل الزامي")]
public int? IDCategory { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "حقل الزامي")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "يجب ادخال ارقام فقط")]
public string Price { get; set; }
[Required(ErrorMessage = "حقل الزامي")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "يجب ادخال ارقام فقط")]
public string Quantity { get; set; }
public List<ImageModel> Images { get; set; } = new List<ImageModel>();
public CategoryModel CategoryModel { get; set; } = new CategoryModel();
public ProductDetailsModel ProductModelDetails { get; set; } = new ProductDetailsModel();
}
public class ProfileMapper : Profile
{
public ProfileMapper()
{
CreateMap<ProductModel, Product>().ReverseMap();
CreateMap<ImageModel, Image>().ReverseMap();
CreateMap<CategoryModel, Category>().ReverseMap();
CreateMap<ProductDetailsModel, ProductDetails>().ReverseMap();
}
}
public async Task Add(ProductModel entity)
{
try
{
var theObject = mapper.Map<ProductModel, Product>(entity);
await Db.Product.AddAsync(theObject);
}
catch (Exception e)
{
await jSRuntime.ToastrError(e.Message);
}
}
private async Task AddProductsForm()
{
await UnitOfWork.ProductRepository.Add(new ProductModel {ProductModelDetails = new ProductDetailsModel { Details="gg"},Images = new List<ImageModel> { new ImageModel { ImageUrl = defaultImage } } });
await UnitOfWork.Complete();
ProductModels = (ICollection<ProductModel>)await UnitOfWork.ProductRepository.GetALL();
}
您的映射不包括名称更改。
例如来自 Category => CategoryModel
CreateMap<Product, ProductModel>()
.ForMember(dest => dest.CategoryModel, opt => opt.MapFrom(src => src.Category))
.ReverseMap();
automapper 总是转换 null 对象并且它不会抛出任何异常这里是我的模型和我使用的方法...当我在 ProductModel 模型中初始化 ProductDetailsModel 时我想要什么我也想在 Product 模型中初始化... ProductModel 中的 ProductDetailsModel 和 CategoryModel 这两个对象都没有在 Product
中初始化 public class Product
{
[Key]
public int ID { get; set; }
public int? IDCategory { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public int Quantity { get; set; }
public List<Image> Images { get; set; }
[ForeignKey("IDCategory")]
public Category Category { get; set; }
public virtual ProductDetails ProductDetails { get; set; }
}
public class ProductModel
{
[Key]
public int ID { get; set; }
[Required(ErrorMessage ="حقل الزامي")]
public int? IDCategory { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "حقل الزامي")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "يجب ادخال ارقام فقط")]
public string Price { get; set; }
[Required(ErrorMessage = "حقل الزامي")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "يجب ادخال ارقام فقط")]
public string Quantity { get; set; }
public List<ImageModel> Images { get; set; } = new List<ImageModel>();
public CategoryModel CategoryModel { get; set; } = new CategoryModel();
public ProductDetailsModel ProductModelDetails { get; set; } = new ProductDetailsModel();
}
public class ProfileMapper : Profile
{
public ProfileMapper()
{
CreateMap<ProductModel, Product>().ReverseMap();
CreateMap<ImageModel, Image>().ReverseMap();
CreateMap<CategoryModel, Category>().ReverseMap();
CreateMap<ProductDetailsModel, ProductDetails>().ReverseMap();
}
}
public async Task Add(ProductModel entity)
{
try
{
var theObject = mapper.Map<ProductModel, Product>(entity);
await Db.Product.AddAsync(theObject);
}
catch (Exception e)
{
await jSRuntime.ToastrError(e.Message);
}
}
private async Task AddProductsForm()
{
await UnitOfWork.ProductRepository.Add(new ProductModel {ProductModelDetails = new ProductDetailsModel { Details="gg"},Images = new List<ImageModel> { new ImageModel { ImageUrl = defaultImage } } });
await UnitOfWork.Complete();
ProductModels = (ICollection<ProductModel>)await UnitOfWork.ProductRepository.GetALL();
}
您的映射不包括名称更改。 例如来自 Category => CategoryModel
CreateMap<Product, ProductModel>()
.ForMember(dest => dest.CategoryModel, opt => opt.MapFrom(src => src.Category))
.ReverseMap();