C
C#7d ago
`

im watching a course about asp.net and creating some kind of web

but everytime i try to do "dotnet ef migrations add init" it says "Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[api.Data.ApplicationDBContext]' while attempting to activate 'api.Data.ApplicationDBContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728"
Search - Microsoft Bing
In a hole, there was a story
Today is Tolkien Reading Day. It's a time to dust
No description
No description
No description
No description
23 Replies
Keswiik
Keswiik7d ago
$code
MODiX
MODiX7d ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
`
`OP7d ago
why ?
// using api.Models;
using Microsoft.EntityFrameworkCore;

namespace api.Data
{
public class ApplicationDBContext : DbContext
{

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


public DbSet<Stock> Stock { get; set; }
public DbSet<Comment> Comments { get; set; }
}
}
// using api.Models;
using Microsoft.EntityFrameworkCore;

namespace api.Data
{
public class ApplicationDBContext : DbContext
{

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


public DbSet<Stock> Stock { get; set; }
public DbSet<Comment> Comments { get; set; }
}
}
// namespace api.Models
{
public class Comment
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public DateTime CreatedOn { get; set; } = DateTime.Now;
public int? StockId { get; set; }
//navigation property
public Stock? Stock { get; set; }
}
}
// namespace api.Models
{
public class Comment
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public DateTime CreatedOn { get; set; } = DateTime.Now;
public int? StockId { get; set; }
//navigation property
public Stock? Stock { get; set; }
}
}
// using System.Net.Mime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;


namespace api.Models
{
public class Stock
{
public int Id { get; set; }
public string Symbol { get; set; } = string.Empty;
public string CompanyName { get; set; } = string.Empty;
[Column(TypeName = "decimal(18,2)")]
public Decimal Purchase { get; set; }
[Column(TypeName = "decimal(18,2)")]
public Decimal LastDiv { get; set; }
public string Industry { get; set; } = string.Empty;
public long MarketCap { get; set; }
public List<Comment> Comment { get; set; } = new List<Comment>();
}
}
// using System.Net.Mime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;


namespace api.Models
{
public class Stock
{
public int Id { get; set; }
public string Symbol { get; set; } = string.Empty;
public string CompanyName { get; set; } = string.Empty;
[Column(TypeName = "decimal(18,2)")]
public Decimal Purchase { get; set; }
[Column(TypeName = "decimal(18,2)")]
public Decimal LastDiv { get; set; }
public string Industry { get; set; } = string.Empty;
public long MarketCap { get; set; }
public List<Comment> Comment { get; set; } = new List<Comment>();
}
}
@ded like this?
Keswiik
Keswiik7d ago
ya this all looks fine have you configured your asp app to have a dbcontext in your Startup.cs file?
`
`OP7d ago
am i suppose to have a startup.cs file
Keswiik
Keswiik7d ago
in most cases, yes
`
`OP7d ago
No description
Keswiik
Keswiik7d ago
that is what asp will use to register services and configure the server
`
`OP7d ago
program?
Keswiik
Keswiik7d ago
show what's in program.cs also, fwiw, i would recommend using visual studio if you are new to c# not VSC
`
`OP7d ago
ik i was jsut watching a course and it did it in vscode so i didnt wanna do anything wrong
// using api.Data;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

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

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.Run();
// using api.Data;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

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

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.Run();
Keswiik
Keswiik7d ago
oh, top level statement asp project
`
`OP7d ago
hm? wdym?
Keswiik
Keswiik7d ago
it isn't using the asp web host, so I'm guessing that's why you're getting that error
`
`OP7d ago
how do i use it?
`
`OP7d ago
okay thanks ill see @Keswiik yo actually tysm bro ive been trying to make this work for litteraly 3 days
`
`OP7d ago
No description
Keswiik
Keswiik7d ago
`
`OP7d ago
okay thank you
Keswiik
Keswiik7d ago
:PepoSalute:
glhays
glhays6d ago
Can you show the api.csproj file in this screenshot
SG97
SG976d ago
I think they're banned

Did you find this page helpful?