C
C#12mo ago
candrd

EF6+.NET Framework+ Postgre issue

Maybe the title is confusing but i need help, i want a really basic code first app. I researched 3 days but i can't find any solution to this problem. When i try to enable-migrations PMC gives me this error. I can share my codes if you want. My connectionString is true.So don't ask me this please.
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.
32 Replies
Angius
Angius12mo ago
Seeing the code would be nice, yes Also, ensure Postgres is actually running
candrd
candrdOP12mo ago
Postgres is running, my java db is working rn Kisiler.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RehberAppNew
{
public class Kisiler
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int Number { get; set; }
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RehberAppNew
{
public class Kisiler
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int Number { get; set; }
}
}
Jimmacle
Jimmacle12mo ago
your dbcontext registration/instantiation is the important part here
candrd
candrdOP12mo ago
AppContext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RehberAppNew
{
public class AppContext : DbContext
{
AppContext(): base("Name=AppContext") { }
DbSet<Kisiler> Kisilers { get; set; }
}
}

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RehberAppNew
{
public class AppContext : DbContext
{
AppContext(): base("Name=AppContext") { }
DbSet<Kisiler> Kisilers { get; set; }
}
}

Jimmacle
Jimmacle12mo ago
so where is the dbcontext getting the connection string from? or is old EF weird and magic about how it works
candrd
candrdOP12mo ago
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="AppContext" providerName="Npgsql" connectionString="Server=localhost;Database=directory;User Id= postgres;Password=pst.grs" />
</connectionStrings>
</configuration>

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="AppContext" providerName="Npgsql" connectionString="Server=localhost;Database=directory;User Id= postgres;Password=pst.grs" />
</connectionStrings>
</configuration>

Jimmacle
Jimmacle12mo ago
i have to ask, is there a reason you're using such an outdated version of .NET?
candrd
candrdOP12mo ago
app.config
Angius
Angius12mo ago
Ah, it's 4.8 I know nothing about it, so you'll have to rely on our resident archaeologists for help lol
candrd
candrdOP12mo ago
how can i use in winform application otherwise?
Jimmacle
Jimmacle12mo ago
you can use winforms in .NET 8
candrd
candrdOP12mo ago
wait a second how?
Angius
Angius12mo ago
Unless you mean webforms
Jimmacle
Jimmacle12mo ago
i personally wouldn't, but there's nothing stopping you
Angius
Angius12mo ago
dotnet new winforms
Jimmacle
Jimmacle12mo ago
<UseWindowsForms>true</UseWindowsForms> in your csproj or that
Angius
Angius12mo ago
Or $newproject
MODiX
MODiX12mo ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
Jimmacle
Jimmacle12mo ago
that would be another whole tier of issue
Angius
Angius12mo ago
Winforms are old, yes, but still supported Webforms are dead, and good riddance
Jimmacle
Jimmacle12mo ago
well not the ones running at my job
Jimmacle
Jimmacle12mo ago
painfroggo
Angius
Angius12mo ago
My condolences
candrd
candrdOP12mo ago
actually i made with mvc model but i have to made with with windows forms So can i work with efcore in .net 8 ?
Angius
Angius12mo ago
Of course
candrd
candrdOP12mo ago
i can't see any config file here? how can i add connectionstring to my project for npgsql?
No description
Jimmacle
Jimmacle12mo ago
ef core is more flexible, as long as you can get it a DbContextOptions<MyDbContext> with the connection information it'll just work or hardcode it in the context if you're just doing basic testing
candrd
candrdOP12mo ago
Thanks to everyone who tried to help me
candrd
candrdOP12mo ago
Now this is
No description

Did you find this page helpful?