Home > other >  Entity Framework Core save entity with foreign key
Entity Framework Core save entity with foreign key

Time:01-25

I'm learning EF, it's my first ORM so I have some logic issues, hope that you could guide me in this awesome technology.

Thanks in advance.

My classes:

public class Father
{
   [Key]
   public int Id { get; set; }
   
   [Required]
   [Column(TypeName = "nvarchar(150)")]
   public string Name { get; set; }

   public virtual ICollection<Child> child { get; set; }
}

public class Child
{
   [Key]
   public int Id { get; set; }
   
   [Required]
   [Column(TypeName = "nvarchar(150)")]
   public string Name { get; set; }

   public int fatherId { get; set; }
}

First trouble: when I save a Father object, EF Core asks for the Child object, too; how can I avoid that? I don't want to save a Child object when saving a Father object.

I want the following output, so I tried this:

return await _context.Person.Include(p => p.LoanHeader).ToListAsync();

Is this the best way to achieve this?

When I'm building many report types, what is the best way to process my information?

  • Sending the whole object and processing it in front-end (I'm working with Angular with Typescript)

  • Process information at back-end, if so, I have to create many objects as I need?

      {
          "id": 0,
          "name": "string",
          "child": [
              {
                  "id": 0,
                  "personId": 0,
                  "name": "string",
              }
          ]
      }
    

CodePudding user response:

First trouble: when I save a Father object, EF Core asks for the Child object, too; how can I avoid that? I don't want to save a Child object when saving a Father object.

This is down to how your relationships are structured. I know tutorials insist on having the ICollection on the parent object, but I've always found it's easier to rather just reference the parent element on the child.

So my suggestion to address this issue would be to change the structure as follows:

public class Father
{
   [Key]
   public int Id { get; set; }
   
   [Required]
   [Column(TypeName = "nvarchar(150)")]
   public string Name { get; set; }

   // Remove ICollection property.
}

public class Child
{
   [Key]
   public int Id { get; set; }
   
   [Required]
   [Column(TypeName = "nvarchar(150)")]
   public string Name { get; set; }

   // Change father to a navigation property.
   public virtual Father Father { get; set; }
}

Now you can save a Father without having to save any Child, but in order to save a Child, you need to specify a Father.

Your selection would look something like this:

await context.Children.Include(x => x.Father).ToListAsync();

That's how I would do it anyway.

CodePudding user response:

you have to fix relations between classes

public class Father
{
   [Key]
   public int Id { get; set; }
   
   [Required]
   [Column(TypeName = "nvarchar(150)")]
   public string Name { get; set; }

   [InverseProperty(nameof(Child.Father))]
   public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
   [Key]
   public int Id { get; set; }
   
   [Required]
   [Column(TypeName = "nvarchar(150)")]
   public string Name { get; set; }


   public int? FatherId { get; set; }

    [ForeignKey(nameof(FatherId))]
    [InverseProperty("Children")]
    public Father Father { get; set; }
}

after this you will have to make a new clean migration to data base

to create a json that you posted, you need the code like this

return await _context.Fathers.Include(p => p.Children).ToListAsync();
  •  Tags:  
  • Related