ske
ske

Reputation: 5254

How to avoid creating a parent when adding a child record in Entity Framework?

There are 2 tables in database, one is [PARENTS] and the other one is [CHILDREN] - having a ParentId as foreign key.

so the relationship between PARENTS and CHILDREN is one-many.

The problem was when I was trying to creating a new CHILDREN record, it would be creating a new PARENT too.

How to solve it ? Thank you

enter image description here

  public void Add(Model.Pages.PageGroup entity)
        {
            using (var ctx = new SmartDbContext())
            {
                var dbEntity = entity.ToEntity();
                ctx.PageGroups.Add(dbEntity);
                ctx.SaveChanges();
            }
        }

    namespace SmartECM.Repository.EF
    {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class PageGroup
    {
        public PageGroup()
        {
            Pages = new HashSet<Page>();
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid GroupId { get; set; }

        [Required]
        [StringLength(100)]
        public string GroupName { get; set; }

        public Guid ProjectId { get; set; }

        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
        public DateTime? DateAdded { get; set; }
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
        public DateTime? DateUpdated { get; set; }

        public virtual Project Project { get; set; }

        public virtual ICollection<Page> Pages { get; set; }
    }
}


    namespace SmartECM.Repository.EF
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class Project
    {
        public Project()
        {
            PageGroups = new HashSet<PageGroup>();
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid ProjectId { get; set; }

        [Required]
        [StringLength(100)]
        public string ProjectName { get; set; }

        public string ProjectDescription { get; set; }

        public Guid? Owner { get; set; }

        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
        public DateTime? DateAdded { get; set; }

        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)]
        public DateTime? DateUpdated { get; set; }

        public virtual ICollection<PageGroup> PageGroups { get; set; }
    }
}

Upvotes: 3

Views: 1509

Answers (2)

ske
ske

Reputation: 5254

Thanks everyone!!!

I've got the correct answer from this link Entity Framework - Duplicate parent(one) when create new child(many)

It should attach the parent to let the EF can detect it, I guess.

enter image description here

Upvotes: 5

Corey Adler
Corey Adler

Reputation: 16137

Based on what you've posted so far, I think that your problem is coming from where you are setting the values on the entity in question (i.e. before you call the Add() method). What I think that you're doing is that you're setting the Project navigation property on your PageGroup entity, and then trying to save it like that.

The problem with doing it that way is that when you add the PageGroup entity to the collection EF sees that the ProjectId field is null and assume that the data found in the Project navigation property is a brand new Project that is not currently in the system. It will then go right ahead and create that "new" Project in the database in order to add your PageGroup entity to its collection.

The solution then is to just populate the ProjectId field with the ID of the Project that you're trying to associate it to, and not assign anything to the navigation property. That way EF will know which Project you're trying to assign your entity to.

Upvotes: 2

Related Questions