Base solution for your next web application
Open Closed

PARENT / CHILD Relationships #3479


User avatar
0
shimulcse created

Hi, I have two entity Country & CountryPort

A country will have one or more CountryPort.

Exactly did same for the parent-child relationship as per your suggested documents. ( [https://www.aspnetzero.com/Documents/Developing-Step-By-Step-Angular#changing-getpeople-method]))

Without adding Include extension method to the query I am not getting CountryPort data (showing NULL) within Country.

var countries = _CountryRepository.GetAll().Include(c=> c.CountryPorts);

Without adding Include I am getting NULL in countries.CountryPorts

Also, didn't find any option to get CountryPorts for single Country data

var myCountry = _CountryRepository.Get(input.Id);

Will you please suggest.

Thanks Shimul Dey


9 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    EntityFramework Core does not support lazy-loading. Check this: <a class="postlink" href="https://docs.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading">https://docs.microsoft.com/en-us/ef/cor ... zy-loading</a>

    For using eager-loading, check this tutorial (Load one entity and its related entites): <a class="postlink" href="https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113">https://msdn.microsoft.com/en-us/librar ... 2(v=vs.113</a>).aspx

  • User Avatar
    0
    shimulcse created

    Hi, Found problems that related to SoftDelete in PARENT/CHILD relationships.

    var countries = _CountryRepository.GetAll().Include(c=> c.CountryPorts);

    Problem#1: .Include(c=> c.CountryPorts) returning deleted (SoftDelete IsDeleted = 1) CountryPorts data also.

    Problem#2: When deleting (SoftDelete) any Parent Data, system not restricting when there are some Child data with the Id of the parent data.

  • User Avatar
    0
    tteoh created

    I've one problem about the .Include() here.

    The data of the relationship is showed even there is not .Include() extension method. May I know why is this happening? Is it because of the relationship that I created on Entity?

    [Table("AppOrganizationChart")]
        public class OrganizationChart : Entity<long>
        {
            public string Name { get; set; }
            public long? ParentId { get; set; }
            public string Relationship { get; set; }
    
            public int TitleId { get; set; }
            [ForeignKey("TitleId")]
            public virtual Title Title { get; set; }
        }
    

    Thanks. /Tommy

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @tteoh,

    Are the marked as deleted ? If so, Include brings deleted records in current version. We are hoping to fix this with EF Core 2.0 release.

    For now, you can use linq query syntax instead of using Include.

  • User Avatar
    0
    jholla created

    You're over engineering the problem with above solution(s)/proposals:

    It is due to a JSON Serialization issue, and there is two ways to fix it by Ignoring Reference Loop Handling, or JsonIgnore Attribute:

    Number 1: This was the fix to my issue, add this to Startup.cs inside the public void ConfigureServices(IServiceCollection services) method:

    services.AddMvc().AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling
          = ReferenceLoopHandling.Ignore;
    });
    

    Related Articles on the above:

    <a class="postlink" href="https://benohead.com/c-circular-reference-detected-serializing-object/">https://benohead.com/c-circular-referen ... ng-object/</a> ASP.NET Core API only returning first result of list Number 2: If you want to also be able to have your API ready for TCP/IP or MVC/Razor, do not use that and just use the [JsonIgnore] attribute, which is actually a better practice than the code above, see articles below for more info:

    <a class="postlink" href="http://www.newtonsoft.com/json/help/html/serializationattributes.htm#JsonIgnoreAttribute">http://www.newtonsoft.com/json/help/htm ... eAttribute</a>

    <a class="postlink" href="https://stackoverflow.com/questions/10169648/how-to-exclude-property-from-json-serialization">https://stackoverflow.com/questions/101 ... ialization</a>

  • User Avatar
    0
    tteoh created

    <cite>ismcagdas: </cite> Hi @tteoh,

    Are the marked as deleted ? If so, Include brings deleted records in current version. We are hoping to fix this with EF Core 2.0 release.

    For now, you can use linq query syntax instead of using Include.

    No there is no marked as deleted.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @tteoh,

    Can you share the code which causes problem for you ? And also the data you have retrieved you don't want to.

    Thanks.

  • User Avatar
    0
    tteoh created

    <cite>ismcagdas: </cite> Hi @tteoh,

    Can you share the code which causes problem for you ? And also the data you have retrieved you don't want to.

    Thanks.

    Hi, as I mentioned before I'm wondering why the related data is showing even I'm not using Include method

    Entity class:

    [Table("AppOrganizationChart")]
        public class OrganizationChart : Entity<long>
        {
            public string Name { get; set; }
            public long? ParentId { get; set; }
            public string Relationship { get; set; }
    
            public int TitleId { get; set; }
            [ForeignKey("TitleId")]
            public virtual Title Title { get; set; }
        }
    

    Code of getting data:

    return await _organizationChartRepository
                    .GetAll()
                    .ToListAsync();
    

    Output (I get this from swagger):

    [
      {
        "name": "string",
        "parentId": 0,
        "relationship": "string",
        "titleId": 0,
        "title": {
          "titleName": "string",
          "className": "string"
        }
      }
    ]
    

    Thanks. /Tommy

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @tteoh,

    I'm asking just to be sure. The output you shared is the schema of your entity, right ? It is not the data. Or do you get data for that but you shared schema ?

    If so, please send your project to <a href="mailto:[email protected]">[email protected]</a> with an explanation of reproducing your problem and we will take a look at it.

    Thanks.