Hi,
I have two tables table1 and table2. table2 has foreign key relationship with table1. Table2 contains the table1_fk_Id column. Now i am using following code to insert data into both tables:-
Table1 record = _objectMapper.Map<Table1>(input);
await _Table1Repository.InsertAsync(record);
(where input is table1 dto class which contains Navigation Property of table2.)
This code is working fine. It is inserting record in both tables. I want to know how can i update both the tables, because when i tried to update it is only updating table1 record.
Thanks.
4 Answer(s)
-
0
hi @smartlayer
Please share the code of the entity and related Dto.
-
0
Hi,
Following are the two tables :-
table1 -
[Table("Products")] public class Entity_Product : FullAuditedEntity { public const int MaxLength_Name = 50; [MaxLength(MaxLength_Name)] public virtual string Name { get; set; } [ForeignKey("Admin_Site_FK")] public Entity_Admin_Site Entity_Admin_Site { get; set; } public virtual int Admin_Site_FK { get; set; } [ForeignKey("Product_Unit_FK")] public Entity_Product_Unit Entity_Product_Unit { get; set; } public virtual int Product_Unit_FK { get; set; } public virtual int Withdrawl { get; set; } public virtual bool Fixed_Dosage { get; set; } public virtual int Base_Weight { get; set; } public virtual float Unit_Base_Weight { get; set; } public virtual int Default_Duration { get; set; } public virtual int PTI { get; set; } public virtual float Cost { get; set; } public virtual bool Single_Use { get; set; } public virtual bool Treatment { get; set; } public virtual int? TenantId { get; set; } public virtual bool IsActive { get; set; } // Navigation Properties public List<Entity_Product_Pricing> Entity_Product_Pricing { get; set; } }
table2-
[Table("Product_Pricing")] public class Entity_Product_Pricing : FullAuditedEntity { [ForeignKey("Product_Id_FK")] public Entity_Product Entity_Product { get; set; } public virtual int? Product_Id_FK { get; set; } [ForeignKey("Configuration_Detail_Id_FK")] public Entity_Configuration_Detail Entity_Configuration_Detail { get; set; } public virtual int? Configuration_Detail_Id_FK { get; set; } public float Price { get; set; } }
Following are the Dtos :-
Dto of table1 :-
[AutoMapFrom(typeof(Entity_Product))] [AutoMapTo(typeof(Entity_Product))] public class CreateEditProductsInput { public int? Id { get; set; } [Required] [Display(Name = "Product Name")] [MaxLength(Entity_Product.MaxLength_Name)] public string Name { get; set; } [Required(ErrorMessage = "The Admin Site field is required.")] [Display(Name = "Admin Site")] public int Admin_Site_FK { get; set; } [Required(ErrorMessage = "The Product Unit field is required.")] [Display(Name = "Product Unit")] public int Product_Unit_FK { get; set; } [Required(ErrorMessage = "The Withdrawal field is required.")] [Display(Name = "Product Unit")] public int? Withdrawl { get; set; } public bool Fixed_Dosage { get; set; } public int? Base_Weight { get; set; } [Required(ErrorMessage = "The Units/Base Weight field is required.")] [Display(Name = "Product Unit")] public float? Unit_Base_Weight { get; set; } public int? Default_Duration { get; set; } public int? PTI { get; set; } public float Cost { get; set; } public bool Single_Use { get; set; } public bool Treatment { get; set; } public int? TenantId { get; set; } // Navigation Properties public List<CreateEditProductPricingInput> Entity_Product_Pricing { get; set; } }
Dto of table 2 :-
[AutoMapTo(typeof(Entity_Product_Pricing))] [AutoMapFrom(typeof(Entity_Product_Pricing))] public class CreateEditProductPricingOutput: CreateEditProductPricingInput { public long? CreatorUserId { get; set; } public DateTime CreationTime { get; set; } public long? LastModifierUserId { get; set; } public DateTime? LastModificationTime { get; set; } }
Thanks
-
0
There are a lot of related discussions about update one to many(parent and children) here. https://github.com/aspnetboilerplate/aspnetboilerplate/issues/4817 https://github.com/aspnetboilerplate/aspnetboilerplate/issues/4715 https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3470 https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2920 https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2170
-
0
This issue is closed because of no recent activity. Please open a new issue if you are still having this problem.