Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:08:29+00:00 2026-05-24T11:08:29+00:00

Framework: I’m using using MVC 3 + EntityFramework 4.1 Code-First. Concept: One Legislation entity

  • 0

Framework: I’m using using MVC 3 + EntityFramework 4.1 Code-First.

Concept: One Legislation entity has many Provision entities. The idea is that the user enters a Legislation entity, that gets saved then the function that saves it passes it along to another function to see whether that Legislation has a ShortTitle. If it does, then it formats it into a properly worded string and includes it as the Legislation’s first Provision, then saves the changes to db.

Issue: The problem is, I’ve tried coding it in different ways, I keep getting a NullReferenceException, telling me to create a new object instance with the “new” keyword, and points me to the savedLegislation.Provisions.Add(provision); line in my second function.

Here are the two functions at issue, this first one saves the Legislation proper:

public Legislation Save(NewLegislationView legislation)
{
    Legislation newLegislation = new Legislation();
    // Simple transfers
    newLegislation.ShortTile = legislation.ShortTile;
    newLegislation.LongTitle = legislation.LongTitle;
    newLegislation.BillType = legislation.BillType;
    newLegislation.OriginatingChamber = legislation.OriginatingChamber;
    newLegislation.Preamble = legislation.Preamble;

    // More complicated properties
    newLegislation.Stage = 1;
    this.NumberBill(newLegislation); // Provides bill number
    newLegislation.Parliament = db.LegislativeSessions.First(p => p.Ending >= DateTime.Today);
    newLegislation.Sponsor = db.Members.Single(m => m.Username == HttpContext.Current.User.Identity.Name);

    // And save
    db.Legislations.Add(newLegislation);
    db.SaveChanges();

    // Check for Short titles
    this.IncludeShortTitle(newLegislation);

    // return the saved legislation
    return newLegislation;
}

And the second function which is invoked by the first one deals with checking whether ShortTitle is not empty and create a Provision that is related to that Legislation, then save changes.

public void IncludeShortTitle(Legislation legislation)
{
    var savedLegislation = db.Legislations.Find(legislation.LegislationID);
    if (savedLegislation.ShortTile.Any() && savedLegislation.ShortTile.ToString().Length >= 5)
    {
        string shortTitle = "This Act may be cited as the <i>" + savedLegislation.ShortTile.ToString() + "</i>.";
        var provision = new Provision()
        {
            Article = Numbers.CountOrNull(savedLegislation.Provisions) + 1,
            Proponent = savedLegislation.Sponsor,
            Text = shortTitle
        };
        savedLegislation.Provisions.Add(provision);
        db.SaveChanges();
    }
}

I’ve been researching how SaveChanges() works and whether it is properly returning the updated entity, it does (since I get no issue looking it up in the second function). If it works properly, and the legislation is found and the provision is newly created in the second function, I don’t see what is the “null” reference it keeps spitting out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:08:30+00:00Added an answer on May 24, 2026 at 11:08 am

    The null reference in this case would be savedLegislation.Provisions. The Provisions collection won’t be initialized to a new List<Provision> when EF returns your Legislation instance from the db.Legislations.Find(...) method.

    The first thing I’d try is something like this:

    var savedLegislation = db.Legislations
        .Include("Provisions")
        .First(l => l.LegislationID == legislation.LegislationID);
    

    … but I’d also consider just using the legislation instance that was passed into the method rather than fetching it from the database again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Entity Framework magically interprets the following table structure as a many-to-many relationship. table foo
Entity framework is cripplingly slow so I tried using a stored procedure but I
Entity Framework 4, POCO objects and ASP.Net MVC2. I have a many to many
Entity framework provides great flexibility to update data in the same datacontext Dim personA
After downloading all .NET framework symbols and sources using NetMassDownloader , is it possible
This is entity framework 4. CurrentProperty.FMVHistories.Add(FMVPresenter.GetFMVHistoryObject()); DataLayer.AccrualTrackingEntities repository = new AccrualTrackingEntities(); repository.Properties.AddObject(CurrentProperty); repository.SaveChanges(); Right
I know AS3Commons-collection framework is a good one, but it can't use in ComboBox
I am using Zend Framework 1.6 hence I'm not utilizing Zend_Application. I have a
This is for Entity Framework for .NET 3.5: I have the need to query
.NET Framework: 2.0 Preferred Language: C# I am new to TDD (Test Driven Development).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.