admin管理员组文章数量:1431721
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 badges1 Answer
Reset to default 1The 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
本文标签: cTrouble when starting EF Core migrationStack Overflow
版权声明:本文标题:c# - Trouble when starting EF Core migration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584446a2664813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论