【问题标题】:problem adding complex objects to database from angular从角度向数据库添加复杂对象的问题
【发布时间】:2019-10-11 02:08:33
【问题描述】:

我正在尝试向数据库添加组 ID,但我的数据模型的类型为“Group”,如何在不发送整个对象的情况下添加 GroupId

这是我的数据模型类

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; }

使用 Entity Framework 核心,我能够将此类作为表添加到数据库中,当然 ef 会将该组对象作为表中的 GroupId

所以现在在我的 Angular 应用程序中,我正在尝试使用此方法向数据库添加一个新工具

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);
      });
  }

现在,每当我添加一个新工具时,它都会起作用,并且 groupId 会被该组填充,但同时,该组会添加到数据库中的组表中。

我尝试仅从 Angular 发送 GroupId,但我收到一条错误消息,提示该工具需要一个 Group 对象而不是字符串。

我的问题是是否可以在不发送整个组对象的情况下添加具有 groupId 的工具?

【问题讨论】:

    标签: c# angular asp.net-core entity-framework-core asp.net-core-2.0


    【解决方案1】:

    您的数据模型类和组对象中需要有一个GroupId 属性。 这样,您可以将GroupId 发送到客户端或从客户端发送,还可以访问数据模型类中的Group 导航属性:

    public int GroupId { get; set; }
    

    【讨论】:

    • 感谢您的回复,我已经尝试过了,但是每当我添加一个新工具时,它也会要求我发送一个组对象
    猜你喜欢
    • 2018-04-15
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多