✅ 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
43 Replies
Please help
Are you just beginning learning backend?
I've watched several tutorials and made a connection but it is confusing me asf
I suggest you to shy away fromalot of the visual studio tools. This is mainly because you wont learn as much heheheh
so what should I use?
I was in your shoes for 2 months ago
what do you mean by proper server?
In SSMS Studio I have this kind of server:
and in Visual Studio only connection I can perform is to localdb using SqlServerExpress
I think the server is your pc basically. It is not a real server from a network provider
am i right?
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
i assume you use .Net and entity framework?
yes
you do have a DbContext??
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; }
}
}
what sql language do you use?
wdym?
SQL is SQL
SQLite, postgreSQL ...
it shows they are using SQL server express (mssqllocaldb) in the screenshot
SQLEXPRESS
What type of project is this? Simple console app? Asp.net project?
asp.net
This code connects to the database
if you want to connect to sqllite, sure, but that is invalid for sql localdb
yeah i'm using sql localdb
DbContext Lifetime, Configuration, and Initialization - EF Core
Patterns for creating and managing DbContext instances with or without dependency injection
there are a few ways you can configure a dbcontext with a connection string, this is an option since you're using asp.net
Yes I have the same kind of code. However - where do I read values such as Server (name), Initial Folder etc.?
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
I think your connection string needs to be something like
(localdb)\\mssqllocaldb;<rest_of_your_stuff_here>
but I never use localdb, could be wrongI 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?
What does your startup file for asp look like?
this is Program.cs as I'm into .net core 7
not sure, I didn't use any of that tooling when I worked with VS
If you are wanting to create your DB though, you can call https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.migrate?view=efcore-8.0
RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade) Method (...
Applies any pending migrations for the context to the database. Will create the database if it does not already exist.
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
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?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'.
I'd put a breakpoint where you configure your dbcontext and make sure the connection string looks the way you expect
!close
Closed!