简体   繁体   中英

problem adding complex objects to database from angular

I'm trying to add a group Id to the database but my data model has type "Group" how can I achieve adding a GroupId without sending the whole object

Here is my Data model class

public class Tool : IObjectBase
    {
        public Group Group { get; set; } // My problem is here 

        public string Code { get; set; }
        public string Name { get; set; }
        public string Statut { get; set; }
        public string StorageLocation { get; set; }
        public string Description { get; set; }
        public DateTime? CalibrationDate { get; set; }
        public string InventoryNumber { get; set; }
        public string SerialNumber { get; set; }
        public string Contact { get; set; }
        public string Image { get; set; }
        public string StandardLoanPeriod { get; set; }

using Entity Framework core I was able to add this class as a table to the database and of course ef will make that group object as an GroupId in the table

So now in my angular application, I am trying to add a new Tool to the database using this Method

AddNewTool(form: NgForm) {
    this.tool = {
      Id :  form.value.Id,
      Code: form.value.Code,
      Name: form.value.Name,
      Description: form.value.Description,
      Image: '',
      Group: this.Group // Probleme is here 
    };
    this.service.AddTool(this.tool).subscribe(
      res => {
        this.ToolToReload.emit(this.service.getTools());
        this.toolRowInformation = null;
        console.log(res);
        form.resetForm();
      } ,
      err => {
          console.log(err);
      });
  }

Now, whenever I add a new tool it works and the groupId get filled with that group but at the same time, that group added to the group table in the database.

I tried sending only the GroupId from angular but I get an error telling that the Tool is Expecting a Group object not a string.

My question is is it possible to add a tool with a groupId without sending the whole Group Object?

You need to have a GroupId property in your data model class as well as the group object. This way, you can send the GroupId to and from the client and also access the Group navigation property in your data model class:

public int GroupId { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM