fbpx

EntityFrameworkCore. Basic exercice. Problem with add-migration.

More
7 months 1 week ago #1 by Guy DEPREZ
Hi All,

I'm using X# Core and I'm trying to do a very basic exercice that you can find here:

www.entityframeworktutorial.net/efcore/e...ole-application.aspx .

(I've no problems when I do this exercice using C#. Maybe it's only a connection string problem ?)


Problem occurs when i apply: add-migration CreateSchoolDB

I get the following error:

"Constructor not found. Unable to find an appropriate constructor for the type System.Runtime.Versioning.FrameworkName."

(I've not the problem when I do it in C#)

NuGet packages are installed
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools

About using:

USING System
USING System.Collections.Generic
USING System.Linq
USING System.Data.Linq
USING system.data.SqlClient
USING System.Text
USING System.Threading.Tasks
USING Microsoft.EntityFrameworkCore
USING Microsoft.EntityFrameworkCore.Design

I created the following classes from the c# code:

CLASS Student

PUBLIC PROPERTY StudentID AS INT AUTO
PUBLIC PROPERTY Name AS STRING AUTO


CONSTRUCTOR()
RETURN

END CLASS

CLASS Course

PUBLIC PROPERTY CourseID AS INT AUTO
PUBLIC PROPERTY CourseName AS STRING AUTO


CONSTRUCTOR()
RETURN

END CLASS

CLASS SchoolContext INHERIT DbContext

PUBLIC PROPERTY Students AS DbSet<Student> AUTO
PUBLIC PROPERTY Courses AS DbSet<Course> AUTO

PROTECTED OVERRIDE METHOD OnConfiguring( optionsBuilder AS DbContextOptionsBuilder) AS VOID

LOCAL cString AS STRING

cString:= e"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;"
optionsBuilder.useSqlServer(cString)
END CLASS

Is the connection string ok?

in c#:
optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;")


I tried with:

e"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;"
"Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;"
"Server=localhost\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;"

Thank you

Guy DEPREZ

Please Log in or Create an account to join the conversation.

More
7 months 1 week ago #2 by Johan Nel
Hi Guy,
Just add a CONSTRUCTOR to your SchoolContext class.

If you look at the previous chapter in that link you will see there is a constructor for that class in the example.

Regards,
Johan

______________________
Johan Nel
George, South Africa

Please Log in or Create an account to join the conversation.

More
7 months 1 week ago #3 by Robert van der Hulst
Replied by Robert van der Hulst on topic EntityFrameworkCore. Basic exercice. Problem with add-migration.
Guy
Remove the e prefix for the connection string. This is not needed and will cause the \S in the strings to be interpreted as a control character The @ notation in C# is the same as our normal strings.

Robert

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

More
7 months 1 week ago #4 by Guy DEPREZ
Thank you Johan and Robert.
But problem not yet solved. Same error with add-migration

My last changes to the class:


CLASS SchoolContext INHERIT DbContext

PUBLIC PROPERTY Students AS DbSet<Student> AUTO
PUBLIC PROPERTY Courses AS DbSet<Course> AUTO

CONSTRUCTOR() AS VOID
SUPER()
RETURN


PROTECTED OVERRIDE METHOD OnConfiguring( optionsBuilder AS DbContextOptionsBuilder) AS VOID
LOCAL oString AS STRING
SUPER:OnConfiguring(optionsBuilder)

oString:= "Server=.\SQLEXPRESS;Database=SchoolDB;Trusted_Connection=True;"
optionsBuilder.useSqlServer(oString)

PROTECTED OVERRIDE METHOD OnModelCreating(oModelBuilder AS ModelBuilder) AS VOID
SUPER:OnModelCreating(oModelBuilder)

END CLASS

Guy

Please Log in or Create an account to join the conversation.

More
7 months 1 week ago #5 by Nick Friend
A stab in the dark, but if you have more than one project in your solution you'll need to specify the default project in the Package Manager Console.

If there was a problem with the connection string I think it would tell you. You can always be more specific in the add-migration call...
Add-Migration -StartUpProjectName "MyProjectName" -ConnectionString "Data Source=NICK-PORTABLE\CIOSQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True;Persist Security Info=True;MultipleActiveResultSets=True" -ConnectionProviderName "System.Data.SqlClient" MyMigrationName

This is for EF6, but I imagine it's the same or similar for EF core.

HTH

Nick

Please Log in or Create an account to join the conversation.

More
7 months 1 week ago #6 by Guy DEPREZ
Thank you Nick

There is something new... It occurs only once by following your suggestion...

Look at the path.... below (in red). It's a assembly /exe path problem...After ....bin\debug, it restarts with the full path


PM> Add-migration -Name "CreateSchoolDB" -Context "SchoolContext" -Project "EFCTutorials" -debug -verbose

Using project ''.
Using startup project ''.
Build started...
Build succeeded.
E:\Visual Studio 2017\X Sharp Solutions\EFCTutorials\packages\Microsoft.EntityFrameworkCore.Tools.2.1.0\tools\net461\any\ef.exe migrations add CreateSchoolDB --json --context SchoolContext --verbose --no-color --prefix-output --assembly "E:\Visual Studio 2017\X Sharp Solutions\EFCTutorials\EFCTutorials\bin\Debug\E:\Visual Studio 2017\X Sharp Solutions\EFCTutorials\EFCTutorials\bin\Debug\EFCTutorials.exe" --startup-assembly "E:\Visual Studio 2017\X Sharp Solutions\EFCTutorials\EFCTutorials\bin\Debug\E:\Visual Studio 2017\X Sharp Solutions\EFCTutorials\EFCTutorials\bin\Debug\EFCTutorials.exe" --project-dir "E:\Visual Studio 2017\X Sharp Solutions\EFCTutorials\EFCTutorials\\" --language XSharp --working-dir "E:\Visual Studio 2017\X Sharp Solutions\EFCTutorials" --root-namespace EFCTutorials
System.NotSupportedException: Le format du chemin d'accès donné n'est pas pris en charge.
à System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
à System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
à Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language)
à Microsoft.EntityFrameworkCore.Tools.AppDomainOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language)
à Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor()
à Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute()
à Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
à Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

Le format du chemin d'accès donné n'est pas pris en charge.
= The format of the given path is not supported.


Guy

Please Log in or Create an account to join the conversation.