【问题标题】:how connect both PostgreSQL and timescale databases into ASP.NET MVC web.config file如何将 PostgreSQL 和 timescale 数据库连接到 ASP.NET MVC web.config 文件
【发布时间】:2018-11-24 11:30:37
【问题描述】:

我使用 ASP.NET MVC 创建了一个项目“SmartHome”。我使用了两种类型的数据库;

  • PostgreSQL
  • 时间尺度

在这里,我将 TIMESCALE 与 PostgreSQL 集成。

我将 PostgreSQL 与 ASP.NET MVC 连接起来,它工作正常。连接字符串如下。

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

这里的数据库名称;

  • PostgreSQL = 智能家居
  • TIMESCALE = smarthomeData

我的问题是“TIMESCALE 数据库如何连接到与 ASP.NET MVC 中相同的“SmartHome”数据库一起工作?

我尝试了 TIMESCALE 的连接字符串,如下所示。

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

但它显示以下错误: “已添加条目‘SmartHomeContext’。”

请帮助或指导我,我是新手。

【问题讨论】:

标签: asp.net asp.net-mvc postgresql


【解决方案1】:

上述错误(“已添加条目'SmartHomeContext'。”)已解决,连接也已完成。步骤如下;

  • Web.config 文件:为 TIMESCALE 数据库添加另一个连接字符串为 "SmartHomeContext1"。在下面的代码sn-p中,第三个连接字符串

    <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>
    
  • 在模型中创建“timescalecontext.cs”文件作为附加图像。 timescalecontext.cs in the Models folder in visual studio

  • 我已经在数据库“smarthomeData”中创建了一个表作为附件。 timescale database
  • 然后,将“timescalecontext.cs”文件编码如下。

    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; }
    }
    
    }
    
  • 由于在进行上述连接之前已经创建了控制器文件,因此将与 Dbcontext 匹配的代码更改为如下:

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

毕竟,它奏效了。希望,这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2013-04-19
    • 2013-05-02
    相关资源
    最近更新 更多