2

Yesterday I started to learn ASP.NET 5 MVC 6 with Entity Framework 7.0.0-rc1-final and I followed this tutorial : Link. The problem is when I run it it throws a NullPointerException on this function:

 if (serviceProvider.GetService<IRelationalDatabaseCreator>().Exists())
        {//adding some entities }

My connection string is: "Server=(localdb)\\MSSQLLocalDB; Database=Test; Trusted_Connection=True; MultipleActiveResultSets=true"

In Startup.cs I have this code:

public void ConfigureServices(IServiceCollection services)
 {
      services.AddEntityFramework()
              .AddSqlServer()
              .AddDbContext<Models.HRPContext>(options =>
              {
                  options.UseSqlServer(Configuration["Data:ConnectionString"]);
              });
      ....... 
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
      Utils.AddSampleData.Initialize(app.ApplicationServices);
      ......
}

The database Test is not yet created. Is there someone who solved this exception? Thanks

  • There is no need to copy tags into the question title. You should come up with a better title to your question. asp.net-5 and entity-framework-7 are the tags, so they are redundant in the title, nor they are making a good title. – Andrew Savinykh Jan 08 '16 at 12:33
  • Also, there is no way you can get NullPointerException in .net unless you create one yourself. NullPointerExceptions are from java. It is important to be precise in your questions, so that who can potentially help you better understand you. You probably mean NullReferenceException. – Andrew Savinykh Jan 08 '16 at 12:36
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Andrew Savinykh Jan 08 '16 at 12:37
  • Yes you're right about Exception, is NullReferenceException my bad. About the title this is my first question. Thanks for feedback :D . About the duplicate: I know what is a NullReferenceException but the EF 7 is a new tehnology... I'm not the only one with this problem and I'm trying to understand why the RelationalDatabaseCreator is not attached to the service in ASP.NET 5 MVC 6 – Matei Nenciu Jan 08 '16 at 13:02
  • I had this Exception because my DbContext was generic and override `IdentityDbContext` and `EF7` cannot create primaries keys, can you share your DbContext ? I solved it, take a look [there](https://github.com/aguacongas/chatle/blob/dev/src/ChatLe.Repository.Identity/ChatleDbContext.cs), the magic is on: where TKey: IEquatable, where TMessage : Message – agua from mars Jan 08 '16 at 13:25

2 Answers2

3

That tutorial is out of date. Use this one instead.

In the updated tutorial, the if statement doesn't exist. Instead, it checks whether a database exists by checking the Database property of the context object:

if (context.Database == null)
{
    throw new Exception("DB is null");
}
greg84
  • 7,541
  • 3
  • 36
  • 45
-1

You'd want to debug your application to solve this, put a breakpoint on this line

if (serviceProvider.GetService<IRelationalDatabaseCreator>().Exists())

and check which member is null. I suspect you haven't registered IRelationalDatabaseCreator or for whatever reasons it was not picked up by your IoC container.

oleksii
  • 35,458
  • 16
  • 93
  • 163
  • I've debuged the app and I've noticed after it passes services registration in the ConfigureServices(..) (services.addEntity...,services.AddMvc...) the serviceType Microsoft.Data.Entity.Storage.IRelationalDatabaseCreator implementationType is null. Could this be the problem? – Matei Nenciu Jan 08 '16 at 12:04