Home > database >  EF Core update-database Build Failed
EF Core update-database Build Failed

Time:09-28

I created a very simple app, and thought I configured everything as it should.

I installed Entity Framework Core for SQL Server and Tools nuget packages.

I created 2 model classes:

public class Make
{
    public int Id { get; set; }
    public string Name { get; set; }    
}

public class Model
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Make Make { get; set; }

    [ForeignKey("Make")]
    public int MakeFK { get; set; }
}

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

    public DbSet<Make> Makes { get; set; }
    public DbSet<Model> Models { get; set; }
}

In Program.cs I added these lines of code:

builder.Services.AddDbContext<AppDbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});

And in appsettings.json, I added the connection string:

"ConnectionStrings": {
    "DefaultConnection": "Data Source =localhost; Database=vRoom; Integrated Security=True; Pooling=False"
},

Now I did all this multiple times in the past and have had no issues. When I build I get no errors.

In the console I run:

 add-migration "initial"

Folder Migrations is created, with _initial, and also Snapshot.cs file.

After the first command I run:

 update-database 

But I get a BuildStart.... then BuildFailed error (nothing else)

I try to build the solution again and now I get an error. In the Migrations Snapshot.cs file there is something that was created and it gives me an error:

[DbContext(typeof(AppDbContext))]

Error says

AppDbContext is a namespace but is used as a type

I have not seen this before even though I've ran probably a dozen migrations in my other app.

Any pointers?

EDIT: I noticed in my older project that I had EF 6.0.8, the version I'm using today is 6.0.9

CodePudding user response:

The reason why you got this error is class and namespace have the same name.

Your project structure may be:

namespace xxx.AppDbContext 
{
    public class AppDbContext 
    {
    }
}

The solution to this issue is make class and namespace have different name:

namespace xxx.MyDbContext 
    {
        public class AppDbContext 
        {
        }
    }

You can read Do not name a class the same as its namespace to Learn More details

CodePudding user response:

I assume that you have folder structure like below:

Project > Model > AppDbContext

as per above folder structure you class was generated like below:

namespace Project.Model.AppDbContext // this name space cause your migration as per error message
{
   public class AppDbContext : DbContext
   {
       public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
       {
       }

       public DbSet<Make> Makes { get; set; }
       public DbSet<Model> Models { get; set; }
   }
}

Change your namespace Project.Model.AppDbContext with namespace Project.Model.AppDbContextABC then it will work fine.

  • Related