"Object reference not set to an instance of an object" when creating a new Web API controller with EF Scaffolding in Visual Studio 2012

asp.net-mvc-4 asp.net-web-api ef-code-first entity-framework visual-studio-2012

Question

I'm working on a Web API/Entity Framework project with a Code First data model. I receive an alert that reads, "Object reference not set to an instance of an object," when I attempt to construct a new API Controller with read/write methods utilizing a data context and model.

After some research, I learned that several causes include incomplete installation of the MvcScaffolding nuget package, improper project type Guids in the.csproj file, and one recommendation to install Powershell 3.

I have verified that the MvcScaffolding package is properly installed, checked that all of my project type guids are accurate, and even installed Powershell 3.

All of this has not provided me with a solution. Although it constructed the tables and relationships just fine, all I can think of is that there is a problem with my data context/model. as follows:

Context:

public class PropertySearchContext : DbContext 
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Property>().HasRequired(p => p.Office).WithMany(o => o.Properties).HasForeignKey(p => p.OfficeId);
    }

    public DbSet<Office> Offices { get; set; }
    public DbSet<Property> Properties { get; set; } 
}

Model:

[Serializable]
public class Property
{
    public int PropertyId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Postcode { get; set; }

    public int Bedrooms { get; set; }
    public int Bathrooms { get; set; }

    public string UmbracoNodeId { get; set; }
    public string MainImageUrl { get; set; }
    public string ListingImageUrl { get; set; }
    public int TotalImageCount { get; set; }
    public PropertyType PropertyType { get; set; }
    public PropertyStatus PropertyStatus { get; set; }
    public long Price { get; set; }
    public string ListingUrl { get; set; }

    //Navigation Properties
    public int OfficeId { get; set; }
    public virtual Office Office { get; set; }

    //Meta properties
    public DateTime CreatedAt { get; set; }
    public string CreatedBy { get; set; }
    public DateTime UpdatedAt { get; set; }
    public string UpdatedBy { get; set; }
}

Relationship String:

<add name="PropertySearchContext" connectionString="Data Source=MERCURY\SQLEXPRESS;Initial Catalog=DATABASE_NAME;Integrated Security=False;User ID=dbFakeUser;Password=fakePassword;Connect Timeout=10" providerName="System.Data.SqlClient" />

Any assistance would be extremely appreciated because I've tried every tip and still can't use scaffolding to develop a controller. Making me crazy

Thanks!

1
3
6/8/2013 10:14:42 PM

Accepted Answer

Identified the issue. My business project's model contained a property with a unique enum type. My data model project, but not the business project, was mentioned in my service project. Therefore, including a reference to the business project AND the model allowed me to successfully install scaffold controllers.

I know it seems apparent, but the error message it displays is so useless!

Anyway, I hope this helps anyone who is experiencing the same issue and is unable to resolve it using the other recommendations.

3
6/9/2013 11:37:54 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow