Click here to Skip to main content
15,569,768 members
Home / Discussions / C#
   

C#

 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois24-Nov-22 16:57
professionalKevin Marois24-Nov-22 16:57 
GeneralRe: Entity Framework Core 6 Problem Pin
Dave Kreskowiak24-Nov-22 17:03
mveDave Kreskowiak24-Nov-22 17:03 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois25-Nov-22 20:16
professionalKevin Marois25-Nov-22 20:16 
GeneralRe: Entity Framework Core 6 Problem Pin
Dave Kreskowiak26-Nov-22 6:20
mveDave Kreskowiak26-Nov-22 6:20 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois26-Nov-22 10:20
professionalKevin Marois26-Nov-22 10:20 
GeneralRe: Entity Framework Core 6 Problem Pin
Dave Kreskowiak26-Nov-22 10:24
mveDave Kreskowiak26-Nov-22 10:24 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois26-Nov-22 11:24
professionalKevin Marois26-Nov-22 11:24 
GeneralRe: Entity Framework Core 6 Problem Pin
Dave Kreskowiak26-Nov-22 12:31
mveDave Kreskowiak26-Nov-22 12:31 
This works differently from the older Entity Frameworks. The reason you're getting the login failure is because the database does not exist in SQLEXPRESS yet.

You cannot create the database just by running the code you have, as is. You first have to enable migrations in the project, then create your first migration ("InitialCreate"). Once that is done, you can add the following line your program:
C#
using (var db = new ModelContext())
{
    // Update the database to the latest migration
    db.Database.Migrate();

    // Creating a new department and save it to the database
    var newDept = new Departments();
    newDept.DepartmentId = 1;
    newDept.DepartmentName = "Development";

I highly recommend AGAINST doing this! You are far better off managing and applying migrations using the EF command line tools! You can EASILY make a mistake that will destroy a production database just by running your code at the wrong time and with the wrong connection string!

Migrations Overview - EF Core | Microsoft Learn[^]

On top of that, there's a few mistakes in your code in your initial post above. For example, every DbSet should be DbSet<type> ...
C#
public virtual DbSet<Department> Departments { get; set; }
public virtual DbSet<Employee> Employees { get; set; }

...and there are mispellings in your modelBuilder code, like
C#
entity.ToTable("Employees", "public");


GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois26-Nov-22 12:50
professionalKevin Marois26-Nov-22 12:50 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois27-Nov-22 8:56
professionalKevin Marois27-Nov-22 8:56 
GeneralRe: Entity Framework Core 6 Problem Pin
Dave Kreskowiak27-Nov-22 9:38
mveDave Kreskowiak27-Nov-22 9:38 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois27-Nov-22 10:16
professionalKevin Marois27-Nov-22 10:16 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois27-Nov-22 10:31
professionalKevin Marois27-Nov-22 10:31 
GeneralRe: Entity Framework Core 6 Problem Pin
Dave Kreskowiak27-Nov-22 10:32
mveDave Kreskowiak27-Nov-22 10:32 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois27-Nov-22 10:39
professionalKevin Marois27-Nov-22 10:39 
GeneralRe: Entity Framework Core 6 Problem Pin
Dave Kreskowiak26-Nov-22 12:40
mveDave Kreskowiak26-Nov-22 12:40 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois26-Nov-22 12:50
professionalKevin Marois26-Nov-22 12:50 
AnswerRe: Entity Framework Core 6 Problem Pin
Sam Hobbs24-Nov-22 12:23
Sam Hobbs24-Nov-22 12:23 
GeneralRe: Entity Framework Core 6 Problem Pin
Kevin Marois24-Nov-22 13:22
professionalKevin Marois24-Nov-22 13:22 
QuestioniTextSharp to Append QRCode to an Existing PDF File Pin
Fezih523-Nov-22 22:27
Fezih523-Nov-22 22:27 
AnswerRe: iTextSharp to Append QRCode to an Existing PDF File Pin
Richard Deeming23-Nov-22 23:00
mveRichard Deeming23-Nov-22 23:00 
QuestionTriangles count in Graph Pin
Member 1177893019-Nov-22 16:20
Member 1177893019-Nov-22 16:20 
AnswerRe: Triangles count in Graph Pin
Dave Kreskowiak19-Nov-22 19:03
mveDave Kreskowiak19-Nov-22 19:03 
AnswerRe: Triangles count in Graph Pin
OriginalGriff19-Nov-22 21:20
mvaOriginalGriff19-Nov-22 21:20 
AnswerRe: Triangles count in Graph Pin
Gerry Schmitz20-Nov-22 6:13
mveGerry Schmitz20-Nov-22 6:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.