none
how to map one object to multiple object RRS feed

  • Question

  • Hello,

    I have stuck several days on this modeling I have one Site main and I want multiple SubSites from it I tried but I didn't know how to insert them correctly into database Seeding can any one help me with it .

     here is the Jason format I get

    and here is Seeding code

    and for additional information here is the project  More Info

    Monday, May 25, 2015 5:24 PM

Answers

  • Hello Ayesh,

    I downloaded your project and after checking it, what I understand is that the Sites entity should have many SubSites entities and they are one to many relationship. For this, I changed your SubSite entity as below:

    public class SubSites
    
        {
    
            //public SubSites()
    
            //{
    
            //    Sites = new Sites();
    
            //}
    
            public int Id { get; set; }
    
    
            public string Name { get; set; }
    
            public int SitesID { get; set; }
    
            public virtual Sites Sites { get; set; }
    
    }
    

    Add the SitesID to explicitly specify the foreign key, this is necessary due to the foreign key constraint.

    Of course, the configuration of for the relationship in the SubSites mapper is changed as:

    this.HasRequired(ss => ss.Sites).WithMany(ss => ss.SubSites).HasForeignKey(ss => ss.SitesID).WillCascadeOnDelete(false);

    Also, I modified the Seed method as:

    public void Seed()
    
            {
    
                if (_ctx.Sites.Count() > 0)
    
                {
    
                    return;
    
                }
    
                try
    
                {
    
                    var site = new Sites()
    
                    {
    
                        Time = 0,
    
                        srcLong = 35.909124,
    
                        srcLat = 31.973628,
    
                        destLong = 35.898258,
    
                        destLat = 31.985622
    
                    };
    
                    _ctx.Sites.Add(site);
    
                    _ctx.SaveChanges();
    
                    foreach (var subsitesName in subsites)
    
                    {
    
                        var subsite = new SubSites
    
                        {
    
                            Name = subsitesName,
    
                            SiteId = site.Id
    
                        };
    
                        _ctx.SubSites.Add(subsite);
    
    
                    }
    
                    _ctx.SaveChanges();
    
                }
    
    
                catch (Exception ex)
    
                {
    
                    string message = ex.ToString();
    
                    throw ex;
    
                }
    
            }
    

    The reason to put the SaveChange for Site is that in Entity Framework, the value of identity column is generated by database.

    Then the generated seed data:

    The Sites table:

    Id            Time      othercolumn      othercolumn      othercolumn      othercolumn

    1              0              35.909124            31.973628            35.898258            31.985622

    The SubSites table:

    ID            Name                                    SitesID

    1              gahwet abu al 3abed      1

    2              5amaret el madene        1

    3              al madena al reyadyeh  1

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 26, 2015 3:35 AM
    Moderator

All replies

  • Hello Ayesh,

    I downloaded your project and after checking it, what I understand is that the Sites entity should have many SubSites entities and they are one to many relationship. For this, I changed your SubSite entity as below:

    public class SubSites
    
        {
    
            //public SubSites()
    
            //{
    
            //    Sites = new Sites();
    
            //}
    
            public int Id { get; set; }
    
    
            public string Name { get; set; }
    
            public int SitesID { get; set; }
    
            public virtual Sites Sites { get; set; }
    
    }
    

    Add the SitesID to explicitly specify the foreign key, this is necessary due to the foreign key constraint.

    Of course, the configuration of for the relationship in the SubSites mapper is changed as:

    this.HasRequired(ss => ss.Sites).WithMany(ss => ss.SubSites).HasForeignKey(ss => ss.SitesID).WillCascadeOnDelete(false);

    Also, I modified the Seed method as:

    public void Seed()
    
            {
    
                if (_ctx.Sites.Count() > 0)
    
                {
    
                    return;
    
                }
    
                try
    
                {
    
                    var site = new Sites()
    
                    {
    
                        Time = 0,
    
                        srcLong = 35.909124,
    
                        srcLat = 31.973628,
    
                        destLong = 35.898258,
    
                        destLat = 31.985622
    
                    };
    
                    _ctx.Sites.Add(site);
    
                    _ctx.SaveChanges();
    
                    foreach (var subsitesName in subsites)
    
                    {
    
                        var subsite = new SubSites
    
                        {
    
                            Name = subsitesName,
    
                            SiteId = site.Id
    
                        };
    
                        _ctx.SubSites.Add(subsite);
    
    
                    }
    
                    _ctx.SaveChanges();
    
                }
    
    
                catch (Exception ex)
    
                {
    
                    string message = ex.ToString();
    
                    throw ex;
    
                }
    
            }
    

    The reason to put the SaveChange for Site is that in Entity Framework, the value of identity column is generated by database.

    Then the generated seed data:

    The Sites table:

    Id            Time      othercolumn      othercolumn      othercolumn      othercolumn

    1              0              35.909124            31.973628            35.898258            31.985622

    The SubSites table:

    ID            Name                                    SitesID

    1              gahwet abu al 3abed      1

    2              5amaret el madene        1

    3              al madena al reyadyeh  1

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 26, 2015 3:35 AM
    Moderator
  • Hello Fred,

    I want to ask u when we want query from database to get the sites and subsites in it I use

     public IQueryable<Sites> GetAllCourses()
            {
                return _ctx.Sites
                        .Include("SubSites")
                        .AsQueryable();
            }

    it will get us the sites and subsite in it ??

    Regards,

    Ayesh.

    Wednesday, May 27, 2015 12:56 PM
  • Why do you need IQueryable or AsQueryable()? You don't need either one of them. IEnumerable can be the return type from the method.

    http://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/

    Wednesday, May 27, 2015 1:28 PM
  • Hello Ayesh,

    >>it will get us the sites and subsite in it ??

    Yes, it would return the Sites and its included SubSites, and as DA924 mentioneds, you do not need to use AsQueryable even, Entity Framework would be lazy-loading by default.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 28, 2015 1:51 AM
    Moderator