locked
Unable to access the navigation property of a newly created object RRS feed

  • Question

  • User-540818677 posted

    I am working on an asp.net core MVC framework + entity framework. where i got those model classes:-

    public partial class Submission
    {
        public Submission()
        {
            SubmissionQuestionSubmission = new HashSet<SubmissionQuestionSubmission>();
        }
    
        public long Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime? Created { get; set; }
    
        public virtual ICollection<SubmissionQuestionSubmission> SubmissionQuestionSubmission { get; set; }
        }
    
    public partial class SubmissionQuestion
        {
            public SubmissionQuestion()
            {
                SubmissionQuestionSubmission = new HashSet<SubmissionQuestionSubmission>();
            }
    
            public int Id { get; set; }
            public string Question { get; set; }
    
            public virtual ICollection<SubmissionQuestionSubmission> SubmissionQuestionSubmission { get; set; }
        }
    
    public partial class SubmissionQuestionSubmission
        {
            public int SubmissionQuestionId { get; set; }
            public long SubmissionId { get; set; }
            public bool? Answer { get; set; }
    
            public virtual Submission Submission { get; set; }
            public virtual SubmissionQuestion SubmissionQuestion { get; set; }
        }
    
    public class SubmissionCreate
        {
            public Submission Submission {set; get;}
            public IList<SubmissionQuestion> SubmissionQuestion { set; get; }
    
            public IList<SubmissionQuestionSubmission> SubmissionQuestionSubmission { set; get; }
    
        }

    and i have the following create view:-

    @for (var i = 0; i < Model.SubmissionQuestion.Count(); i++)
                    {
    
                        <div class="form-group">
                            <input asp-for="@Model.SubmissionQuestion[i].Question" hidden />
                            <input asp-for="@Model.SubmissionQuestionSubmission[i].SubmissionQuestionId" hidden />
                            <label class="control-label" style="font-weight:bold">@Model.SubmissionQuestion[i].Question</label><br />
                            <input type="radio" asp-for="@Model.SubmissionQuestionSubmission[i].Answer" value="true" /><span style="color: #4d9b84;font-size:14px;font-weight:bold"> Yes</span><br />
                            <input type="radio" asp-for="@Model.SubmissionQuestionSubmission[i].Answer" value="false" /><span style="color: #4d9b84;font-size:14px;font-weight:bold"> No</span>
    
    
                        </div>
    
                    }

    and the following create post method:-

    public async Task<IActionResult> Create([Bind("Submission,SubmissionQuestionSubmission")] SubmissionCreate sc )
            {
                if (ModelState.IsValid)
                {
                  var newsubmission  =  _context.Submission.Add(sc.Submission);
                    sc.Submission.Created = DateTime.Now;
                    await _context.SaveChangesAsync();
                    foreach (var v in sc.SubmissionQuestionSubmission)
                    {
    
                        v.SubmissionId = sc.Submission.Id;
                        _context.SubmissionQuestionSubmission.Add(v);
                    }
                     await _context.SaveChangesAsync();

    but inside my action method if i try the following sc.Submission.SubmissionQuestionSubmission.FirstOrDefault(a => a.SubmissionQuestion.Question.StartsWith("Are you")).Answer i will get null reference exception where the sc.Submission.SubmissionQuestionSubmission.SubmissionQuestion will be null, although the relation of these objects are defined inside the database.. any advice?

    Friday, June 26, 2020 7:59 PM

All replies

  • User-474980206 posted

    The issue is there is no code in the POCO objects to load the navigation properties. The SaveChanges does not read the nav properties, only writes the changes. You need to use the dbcontext (DbSet entity wrapper) to load the nav properties. 

    https://www.learnentityframeworkcore.com/relationships/managing-one-to-many-relationships

    Saturday, June 27, 2020 4:21 PM
  • User-540818677 posted

    The issue is there is no code in the POCO objects to load the navigation properties. The SaveChanges does not read the nav properties, only writes the changes. You need to use the dbcontext (DbSet entity wrapper) to load the nav properties. 

    https://www.learnentityframeworkcore.com/relationships/managing-one-to-many-relationships

    ok thanks for the info and reply... but how i were able to get the

    sc.Submission.Id

    as the Id was not available before saving the changes?

    Saturday, June 27, 2020 8:25 PM
  • User-474980206 posted

    there is a little code in EF to read autogenerated columns, and update the context. typically the sql generated is something like:

       insert table ....
       select scope_identity()

    so the only value coming back from the insert is the key.

    Sunday, June 28, 2020 11:58 PM
  • User711641945 posted

    Hi johnjohn123123,

    ok thanks for the info and reply... but how i were able to get the

    sc.Submission.Id

    You misunderstand something about model binding.The object you received named sc,its value depends on what you pass from the View.Only if your View contains such input for the property `Id`,the newly created Object could get it.

    So,be sure your View contains such input and it has value inside.If you do not want to display the value on your View,just add hidden attribute:

    <input asp-for="Submission.Id" hidden/>

    Best Regards,

    Rena

    Monday, June 29, 2020 8:57 AM