C
C#14mo ago
fenish

❔ Ef core multiple projects

Why i cannot create migration with dotnet ef migrations add init
No description
15 Replies
fenish
fenishOP14mo ago
using Microsoft.EntityFrameworkCore;
using WebApiTemplate.Data.Entities;

namespace WebApiTemplate.Data.Sql;

public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public required DbSet<Books> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured) return;
var host = Environment.GetEnvironmentVariable("DB_HOST");
var port = Environment.GetEnvironmentVariable("DB_PORT");
var user = Environment.GetEnvironmentVariable("DB_USER");
var password = Environment.GetEnvironmentVariable("DB_PASSWORD");
var database = Environment.GetEnvironmentVariable("DB_DATABASE");

var connectionString = $"Host={host};Port={port};Username={user};Password={password};Database={database}";
optionsBuilder.UseNpgsql(connectionString);
}
}
using Microsoft.EntityFrameworkCore;
using WebApiTemplate.Data.Entities;

namespace WebApiTemplate.Data.Sql;

public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public required DbSet<Books> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured) return;
var host = Environment.GetEnvironmentVariable("DB_HOST");
var port = Environment.GetEnvironmentVariable("DB_PORT");
var user = Environment.GetEnvironmentVariable("DB_USER");
var password = Environment.GetEnvironmentVariable("DB_PASSWORD");
var database = Environment.GetEnvironmentVariable("DB_DATABASE");

var connectionString = $"Host={host};Port={port};Username={user};Password={password};Database={database}";
optionsBuilder.UseNpgsql(connectionString);
}
}
fenish
fenishOP14mo ago
No description
fenish
fenishOP14mo ago
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[WebApiTemplate.Data.Sql.DatabaseContext]' while attempting to activate 'WebApiTemplate.Data.Sql.DatabaseContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time,
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[WebApiTemplate.Data.Sql.DatabaseContext]' while attempting to activate 'WebApiTemplate.Data.Sql.DatabaseContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time,
i want my migrations to inside of Data
mg
mg14mo ago
What directory are you running the command from? If from WebApiTemplate.Data, I think you'll need to specify WebApiTemplate.Application as your startup project
Pobiega
Pobiega14mo ago
Yes, ef commands have two project switches; target project and startup project Target should be where you want the migration to be, and startup should be the one that does the host builder and Di setup
fenish
fenishOP14mo ago
when i run from application it still gives error about something like specify migration project to "application" not data
Pobiega
Pobiega14mo ago
"run from application"?
fenish
fenishOP14mo ago
application dir
Pobiega
Pobiega14mo ago
Usage: dotnet ef migrations add [arguments] [options]

Arguments:
<NAME> The name of the migration.

Options:
-o|--output-dir <PATH> The directory to put files in. Paths are relative to the project directory. Defaults to "Migrations".
--json Show JSON output. Use with --prefix-output to parse programatically.
-n|--namespace <NAMESPACE> The namespace to use. Matches the directory by default.
-c|--context <DBCONTEXT> The DbContext to use.
-p|--project <PROJECT> The project to use. Defaults to the current working directory.
-s|--startup-project <PROJECT> The startup project to use. Defaults to the current working directory.
--framework <FRAMEWORK> The target framework. Defaults to the first one in the project.
--configuration <CONFIGURATION> The configuration to use.
--runtime <RUNTIME_IDENTIFIER> The runtime to use.
--msbuildprojectextensionspath <PATH> The MSBuild project extensions path. Defaults to "obj".
--no-build Don't build the project. Intended to be used when the build is up-to-date.
-h|--help Show help information
-v|--verbose Show verbose output.
--no-color Don't colorize output.
--prefix-output Prefix output with level.
Usage: dotnet ef migrations add [arguments] [options]

Arguments:
<NAME> The name of the migration.

Options:
-o|--output-dir <PATH> The directory to put files in. Paths are relative to the project directory. Defaults to "Migrations".
--json Show JSON output. Use with --prefix-output to parse programatically.
-n|--namespace <NAMESPACE> The namespace to use. Matches the directory by default.
-c|--context <DBCONTEXT> The DbContext to use.
-p|--project <PROJECT> The project to use. Defaults to the current working directory.
-s|--startup-project <PROJECT> The startup project to use. Defaults to the current working directory.
--framework <FRAMEWORK> The target framework. Defaults to the first one in the project.
--configuration <CONFIGURATION> The configuration to use.
--runtime <RUNTIME_IDENTIFIER> The runtime to use.
--msbuildprojectextensionspath <PATH> The MSBuild project extensions path. Defaults to "obj".
--no-build Don't build the project. Intended to be used when the build is up-to-date.
-h|--help Show help information
-v|--verbose Show verbose output.
--no-color Don't colorize output.
--prefix-output Prefix output with level.
fenish
fenishOP14mo ago
ah you mean rgis
Pobiega
Pobiega14mo ago
pay extra attention to -p and -s
fenish
fenishOP14mo ago
okey I'll try thanks
Pobiega
Pobiega14mo ago
you will most likely want -p to be your "data" or "persistance" project and -s to be your api/application project
fenish
fenishOP14mo ago
I'll try thanks
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server