C
C#7mo ago
rv77777

✅ Creation of a database (EF/CF)

Hello. I have models for creating a database using Entity Framework and Code First approach. How in Visual Studio can I connect to a proper server and create database in /Databases folder instead of e.g. master? Or I can use SSMS if it is simplier here .net 7
No description
43 Replies
rv77777
rv77777OP7mo ago
Please help
Keyvan
Keyvan7mo ago
Are you just beginning learning backend?
rv77777
rv77777OP7mo ago
I've watched several tutorials and made a connection but it is confusing me asf
Keyvan
Keyvan7mo ago
I suggest you to shy away fromalot of the visual studio tools. This is mainly because you wont learn as much heheheh
rv77777
rv77777OP7mo ago
so what should I use?
Keyvan
Keyvan7mo ago
I was in your shoes for 2 months ago what do you mean by proper server?
rv77777
rv77777OP7mo ago
In SSMS Studio I have this kind of server:
No description
rv77777
rv77777OP7mo ago
and in Visual Studio only connection I can perform is to localdb using SqlServerExpress
Keyvan
Keyvan7mo ago
I think the server is your pc basically. It is not a real server from a network provider am i right?
rv77777
rv77777OP7mo ago
yes, just local stuff and testing I have ef models and would like a new database. The tutorials are very confusing the same as this VS
Keyvan
Keyvan7mo ago
i assume you use .Net and entity framework?
rv77777
rv77777OP7mo ago
yes
Keyvan
Keyvan7mo ago
you do have a DbContext??
rv77777
rv77777OP7mo ago
Yes: using Microsoft.EntityFrameworkCore; using SchoolManager.Models; namespace SchoolManager.Services { public class DatabaseContext : DbContext { public DatabaseContext(DbContextOptions options) : base(options) { } public DbSet<Student> Students { get; set; } public DbSet<Group> Groups { get; set; } } }
Keyvan
Keyvan7mo ago
what sql language do you use?
rv77777
rv77777OP7mo ago
wdym? SQL is SQL
Keyvan
Keyvan7mo ago
SQLite, postgreSQL ...
Keswiik
Keswiik7mo ago
it shows they are using SQL server express (mssqllocaldb) in the screenshot
Keyvan
Keyvan7mo ago
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Ensure this is only configured if no other configuration is provided
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Data Source=mydatabase.db");
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Ensure this is only configured if no other configuration is provided
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Data Source=mydatabase.db");
}
}
rv77777
rv77777OP7mo ago
SQLEXPRESS
Keswiik
Keswiik7mo ago
What type of project is this? Simple console app? Asp.net project?
rv77777
rv77777OP7mo ago
asp.net
Keyvan
Keyvan7mo ago
This code connects to the database
Keswiik
Keswiik7mo ago
if you want to connect to sqllite, sure, but that is invalid for sql localdb
rv77777
rv77777OP7mo ago
yeah i'm using sql localdb
Keswiik
Keswiik7mo ago
there are a few ways you can configure a dbcontext with a connection string, this is an option since you're using asp.net
rv77777
rv77777OP7mo ago
Yes I have the same kind of code. However - where do I read values such as Server (name), Initial Folder etc.?
Keswiik
Keswiik7mo ago
that example is probably using whatever configuration stuff comes with asp you can hardcode the connection string to what you want if you're just getting started with DB stuff I wouldn't worry about making everything super configurable if you're just learning
rv77777
rv77777OP7mo ago
c#
builder.Services.AddDbContext<DatabaseContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
options.UseSqlServer(connectionString);
}
);

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=.\\SQLEXPRESS;Database=YourDatabaseName;Trusted_Connection=True;"
}
}
c#
builder.Services.AddDbContext<DatabaseContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
options.UseSqlServer(connectionString);
}
);

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=.\\SQLEXPRESS;Database=YourDatabaseName;Trusted_Connection=True;"
}
}
Keswiik
Keswiik7mo ago
I think your connection string needs to be something like (localdb)\\mssqllocaldb;<rest_of_your_stuff_here> but I never use localdb, could be wrong
rv77777
rv77777OP7mo ago
I am using connection string as above and I am confused. There is this SQL Server Object Explorer - and on the left in VS I have sth like "Connected Services" - is this the same?
Keswiik
Keswiik7mo ago
What does your startup file for asp look like?
rv77777
rv77777OP7mo ago
No description
rv77777
rv77777OP7mo ago
c#
using Microsoft.EntityFrameworkCore;
using SchoolManager.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<DatabaseContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
options.UseSqlServer(connectionString);
}
);
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
c#
using Microsoft.EntityFrameworkCore;
using SchoolManager.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<DatabaseContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
options.UseSqlServer(connectionString);
}
);
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
this is Program.cs as I'm into .net core 7
Keswiik
Keswiik7mo ago
not sure, I didn't use any of that tooling when I worked with VS
Keswiik
Keswiik7mo ago
RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade) Method (...
Applies any pending migrations for the context to the database. Will create the database if it does not already exist.
rv77777
rv77777OP7mo ago
Yes I am using Migrations already, however I do not know how do I compose a connection string. into a database I want and folder I want
Keswiik
Keswiik7mo ago
Not sure if there is a way to specify folder for localdb, but you're already doing Database=YourDatabaseName in your config, or are you wanting a separate option in your config to specify the database name?
rv77777
rv77777OP7mo ago
Okay so after a while: I have this kind of connection string: "Server=.\SQLEXPRESS;Database=SchoolManagerDb;Trusted_Connection=True;" After migrating I get an error Keyword not supported: '"server'.
Keswiik
Keswiik7mo ago
I'd put a breakpoint where you configure your dbcontext and make sure the connection string looks the way you expect
rv77777
rv77777OP7mo ago
!close
Accord
Accord7mo ago
Closed!
Want results from more Discord servers?
Add your server