最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Trouble when starting EF Core migration - Stack Overflow

programmeradmin11浏览0评论

To give you some context I have been trying to learn more about .NET and the EF Core ORM. But it seems like the more I try the less I understand.

As of right now I still can't reliable create a boilerplate for a simple application. Sometimes It breaks sometimes It works sometimes I just don't know what to do.

The problem I most encounter most frequently is this one:

Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[MVC___FreeCodeCamp.Data.MyAppContext]' while attempting to activate 'MVC___FreeCodeCamp.Data.MyAppContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see /?linkid=851728

Now the first few times I get it. Hell I even "fixed" it. But thing is.. I don't understand it. Why does it come out? What does it even mean when it tells me I am using a different design pattern.

It probably means I am doing it wrong. And I get that. But I am currently copying a tutorial I saw.

I am not sure if the design pattern is really the problem here.

These are the relevant parts of my code:

The DbContext:

using Microsoft.EntityFrameworkCore;
using MVC___FreeCodeCamp.Models;

namespace MVC___FreeCodeCamp.Data;
    
public class MyAppContext : DbContext
{
    public MyAppContext(DbContextOptions<MyAppContext> options) :base(options) { }
    
    public DbSet<Item> Items { get; set; }
}

The program.cs relevant code:

builder.Services.AddDbContext<MyAppContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultString")));

The connection string:

"ConnectionStrings": {
    "DefaultString": "Server=localhost,1433;Database=FreeCodeCampMvc;User ID=sa;Password=<YourPassword>;Trusted_Connection=False;Encrypt=False;"  
}

Also the simple model in case it's necessary:

namespace MVC___FreeCodeCamp.Models;

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

As you can see is as bare bone as it gets. Yet it doesn't work. Now to be fair I am currently running this app in .NET 9, Ubuntu 24.04 on JetBrains and I have the SQL Server running locally on a docker container. Hence the port in the connection string.

But it has worked in the past! I have all EF Core relevant nugets installed. I just don't understand. Why?

To give you some context I have been trying to learn more about .NET and the EF Core ORM. But it seems like the more I try the less I understand.

As of right now I still can't reliable create a boilerplate for a simple application. Sometimes It breaks sometimes It works sometimes I just don't know what to do.

The problem I most encounter most frequently is this one:

Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[MVC___FreeCodeCamp.Data.MyAppContext]' while attempting to activate 'MVC___FreeCodeCamp.Data.MyAppContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft/fwlink/?linkid=851728

Now the first few times I get it. Hell I even "fixed" it. But thing is.. I don't understand it. Why does it come out? What does it even mean when it tells me I am using a different design pattern.

It probably means I am doing it wrong. And I get that. But I am currently copying a tutorial I saw.

I am not sure if the design pattern is really the problem here.

These are the relevant parts of my code:

The DbContext:

using Microsoft.EntityFrameworkCore;
using MVC___FreeCodeCamp.Models;

namespace MVC___FreeCodeCamp.Data;
    
public class MyAppContext : DbContext
{
    public MyAppContext(DbContextOptions<MyAppContext> options) :base(options) { }
    
    public DbSet<Item> Items { get; set; }
}

The program.cs relevant code:

builder.Services.AddDbContext<MyAppContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultString")));

The connection string:

"ConnectionStrings": {
    "DefaultString": "Server=localhost,1433;Database=FreeCodeCampMvc;User ID=sa;Password=<YourPassword>;Trusted_Connection=False;Encrypt=False;"  
}

Also the simple model in case it's necessary:

namespace MVC___FreeCodeCamp.Models;

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

As you can see is as bare bone as it gets. Yet it doesn't work. Now to be fair I am currently running this app in .NET 9, Ubuntu 24.04 on JetBrains and I have the SQL Server running locally on a docker container. Hence the port in the connection string.

But it has worked in the past! I have all EF Core relevant nugets installed. I just don't understand. Why?

Share Improve this question edited Nov 19, 2024 at 4:50 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 19, 2024 at 3:12 yzkaelyzkael 3412 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The error indicates when the Dependency injection system try to create an instance of MyAppContext it requires DbContextOptions<MyAppContext> options for you defined the constructor of the DbContext as :

public MyAppContext(DbContextOptions<MyAppContext> options) :base(options) { }

However,it cound't find DbContextOptions<MyAppContext> in DI container.

For you've injected the options with the codes:

builder.Services.AddDbContext<MyAppContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultString")));

You could check if you configured the wrong project(which project contains the DbContext class) &startup project (the project you injected dbcontext with builder.Services.AddDbContext) which caused the DI system failed to find required options when you add migration.

You may check this issue related

发布评论

评论列表(0)

  1. 暂无评论