一:公司
public partial class ItemCompanyEntity : EntityFull
{
/// <summary>
/// 工程ID
/// </summary>
[Column(StringLength =20)]
public long ProjectId { get; set; }
/// <summary>
/// 项目公司名称
/// </summary>
[Column(StringLength = 50)]
public string ItemCompanyName { get; set; }
/// <summary>
/// 供应商列表
/// </summary>
///
[Navigate(nameof(ItemProviderEntity.CompanyId))]
public virtual List<ItemProviderEntity> Providers { get; set; }
}
二:供应商
/// <summary>
/// 供应商名称
/// </summary>
[Table(Name = "ad_item_provider")]
[Index("idx_{tablename}_01", nameof(ItemProviderName))]
public partial class ItemProviderEntity : EntityFull
{
/// <summary>
/// 供应商名称
/// </summary>
[Column(StringLength = 50)]
public string ItemProviderName { get; set; }
/// <summary>
/// 项目公司ID
/// </summary>
public long CompanyId { get; set; }
实现方法:
/// <summary>
/// 项目公司名称--新增
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
public async Task<IResultOutput> AddItemCompanyAsync(ItemCompanyAddDto input)
{
try
{
var entity = Mapper.Map<ItemCompanyEntity>(input);
// _companyRepository.DbContextOptions.EnableCascadeSave = true; 级联保存
// var tbref = _fsql.CodeFirst
//.GetTableByEntity(typeof(ItemCompanyEntity))
//.GetTableRef("Providers", true); 检测导航属性
var id = (await _companyRepository.InsertAsync(entity)).Id;
return ResultOutput.Result(id > 0);
}
catch (Exception ex)
{
return ResultOutput.NotOk(ex.Message);
}
}
/// <summary>
/// 项目公司名称--修改
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
public async Task<IResultOutput> UpdateCompanyAsync(ItemCompanyUpdateDto input)
{
try
{
var entity = Mapper.Map<ItemCompanyEntity>(input);
//var id = (await _companyRepository.UpdateAsync(entity));//级联保存默认是追加保存
await _companyRepository.SaveManyAsync(entity, "Providers");//对比表已存在的数据,计算出添加、修改、删除执行
return ResultOutput.Result(true);
}
catch (Exception ex)
{
return ResultOutput.NotOk(ex.Message);
}
}