0

I created a project "SmartHome" using the ASP.NET MVC. I used two types of databases;

  • PostgreSQL
  • TIMESCALE

Here, I integrate the TIMESCALE with the PostgreSQL.

I connected the PostgreSQL with ASP.NET MVC and it is working fine. the connection string as below.

<add name="SmartHomeContext" 
connectionString="Server=127.0.0.1;Port=5432;Database=smarthome;
User Id=postgres;Password=postdata;" providerName="Npgsql" />

here database names of;

  • PostgreSQL = smarthome
  • TIMESCALE = smarthomeData

My problem is " How TIMESCALE database connect to work with same "SmartHome" database in the ASP.NET MVC ? "

I tried the connection string for TIMESCALE as below.

<add name="SmartHomeContext" 
connectionString="Server=127.0.0.1;Port=5432;Database=smarthomeData;
User Id=postgres;Password=postdata;" providerName="Npgsql" />

but it shows the below error: "The entry 'SmartHomeContext' has already been added."

Please help me or guide me, I am very new to do this.

munchi
  • 1
  • 1
  • Possible duplicate of [The entry '' has already been added error](https://stackoverflow.com/questions/6679233/the-entry-has-already-been-added-error) – Shahar Shokrani Nov 22 '18 at 16:26

1 Answers1

0

Above error ("The entry 'SmartHomeContext' has already been added.") was solved and the connection also done. the steps are followed as below;

  • Web.config file : add a another connection string for TIMESCALE database as "SmartHomeContext1". In the below code snippet, third connection string.

    <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source= 
    (LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-SmartHome- 
    20181019034449.mdf;Initial Catalog=aspnet-SmartHome-20181019034449;Integrated 
    Security=True" providerName="System.Data.SqlClient" />
    
    <add name="SmartHomeContext" 
    connectionString="Server=127.0.0.1;Port=5432;Database=smarthome;User 
    Id=postgres;Password=postdata;" providerName="Npgsql" />
    
    <add name="SmartHomeContext1" 
    connectionString="Server=127.0.0.1;Port=5432;Database=smarthomeData;User 
    Id=postgres;Password=postdata;" providerName="Npgsql" />
    </connectionStrings>
    
  • create "timescalecontext.cs" file in the Model as attached image. timescalecontext.cs in the Models folder in visual studio

  • already I created a table in the database "smarthomeData" as attached image. timescale database
  • then, code the "timescalecontext.cs" file as below.

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    
    namespace SmartHome.Models
    {
    public class timescalecontext : DbContext
    {
    
    public timescalecontext() : base("name=SmartHomeContext1")
    {
    }
    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("public");
        base.OnModelCreating(modelBuilder);
    }
    
    public System.Data.Entity.DbSet<SmartHome.Models.DeviceReading> DeviceReadings { get; set; }
    public System.Data.Entity.DbSet<SmartHome.Models.Event> Events { get; set; }
    }
    
    }
    
  • since the controller file already created before make the above connection, change the code matched with the Dbcontext as below:

    namespace SmartHome.Controllers
    {
    public class DeviceReadingsController : Controller
    {
    private timescalecontext db = new timescalecontext();
    

after all that, It worked. Hope, this will be helpful.

munchi
  • 1
  • 1