3

I'm creating an ASP.NET MVC application which uses a PostgreSql database. Model classes are in a different class library. For access to database i'm using Entity Framework + Npgsql.Entityframework from Nuget in the class library. Also i added same links to main project. Configuration settings are in web.config of main project:

<configSections>
    <section name="entityFramework" 
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
             requirePermission="false" />
</configSections>
<entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    <providers>
        <provider invariantName="Npgsql" 
                  type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    </providers>
</entityFramework>
<connectionStrings>
    <add name="NpgsqlContext" 
         providerName="Npgsql" 
         connectionString="Server=127.0.0.1;User Id=BaseId;Password=BasePass;Port=5432;Database=Base;" />
</connectionStrings>
<system.data>
    <DbProviderFactories>
        <add name="Npgsql Data Provider" 
             invariant="Npgsql"
             support="FF" 
             description=".Net Framework Data Provider for Postgresql"
             type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
</system.data>

I created database and some tables in pgAdmin. To access to base and tables i use classes (example): public class NpgsqlContext : DbContext { public NpgsqlContext(): base(nameOrConnectionString: "NpgsqlContext") { }

        public DbSet<BaseArticle> BaseArticles { get; set; }
    }

    [Table("ARTICLE", Schema = "public")]
    public class BaseArticle
    {
        [Key]
        [Column("ID")]
        public int Id { get; set; }

        [Column("DATETIME")]
        public DateTime DateTime { get; set; }

        [Column("TITLE")]
        public string Title { get; set; }

        [Column("BODY")]
        public string Body { get; set; }
    }

NpgsqlContext object is created normally and has right connectionn string (PORT=5432;KRBSRVNAME=name;TIMEOUT=15;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=20;COMMANDTIMEOUT=20;COMPATIBLE=2.2.7.0;HOST=127.0.0.1;USER ID=BasePass;PASSWORD=BasePass;DATABASE=Base), but didn't see any records in datatable in base - DbSet BaseArticle's count equals 0. Meanwhile records are there. Where can i have an error?

And also - in ASP.NET MVC generally impossible to achieve loose coupling between the parts of the application? For example - i created ASP.NET MVC application and carried out classes of access to a database in separate libraries. But i had to indicate links to the packages from Nuget in the main project. How could I avoid it?

1 Answer 1

1

For the solution of my problem i needed to open connection to base in constructor of context:

this.Database.Connection.Open();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.