【问题标题】:Not able to connect to an external database ASP.NET无法连接到外部数据库 ASP.NET
【发布时间】:2020-07-10 00:11:54
【问题描述】:

我正在尝试使用我的 ASP.NET API 连接到外部数据库,但我不断收到此错误:

Microsoft.Data.SqlClient.SqlException: '无效的对象名称'Parkings'。' Microsoft.EntityFrameworkCore.Relational.dll 中出现“Microsoft.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理 对象名称“Parkings”无效。

但是,我可以从我的 nodejs 服务器应用程序连接到数据库,在该应用程序中我使用数据为数据库播种。

代码:

IP、用户名和密码因明显原因未显示

appsettings.json:

{
  "ConnectionStrings": {
    "ParkingContext": "Server=xxx.xxx.xxx.xxx,xxxxx;Database=Parkings;User Id=xxxxx;Password=xxxxxx;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Startup.cs:

public class Startup {
    public Startup(IConfiguration configuration) {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services) {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        services.AddDbContext<ParkingContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("ParkingContext")));
        services.AddScoped<IParkingRepository, ParkingRepository>();
        services.AddSwaggerDocument();
        services.AddCors(options => options.AddPolicy("AllowAllOrigins", builder => builder.AllowAnyOrigin()));
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
        } else {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseSwaggerUi3();
        app.UseSwagger();
        app.UseRouting();

        app.UseEndpoints(endpoints => {
            endpoints.MapControllers();

        });
    }
}

上下文类

public class ParkingContext : DbContext {
    public DbSet<Parking> Parkings { get; set; }
    public DbSet<Entry> Entries { get; set; }
    public ParkingContext(DbContextOptions<ParkingContext> options) : base(options) {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        var connectionString = @"Server=xxx.xxx.xxx.xxx,xxxx;Database=Parkings;User Id=xxxx;Password=xxxx;";
        optionsBuilder.UseSqlServer(connectionString);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        base.OnModelCreating(modelBuilder);
    }
}

【问题讨论】:

  • 根据错误Invalid object name 'Parkings',您的数据库中没有Parkings表。
  • 非常感谢,我以为是指数据库Parkings,但对象确实是那个数据库的表,我的命名不匹配,再次感谢!!!

标签: c# asp.net .net sql-server


【解决方案1】:

您可以尝试在连接字符串中使用InitialCatalog 属性,而不是Database 属性。否则请确保此处正确使用了“Parkings”对象。

【讨论】:

    【解决方案2】:

    首先,我认为您不应该指定连接字符串两次。你在这里指定它:

    services.AddDbContext<ParkingContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("ParkingContext")));
    

    这里:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
            var connectionString = @"Server=xxx.xxx.xxx.xxx,xxxx;Database=Parkings;User Id=xxxx;Password=xxxx;";
            optionsBuilder.UseSqlServer(connectionString);
        }
    

    第一个就足够了。

    EntityFrameWork 是一个 ORM,因此您需要确保您的 DbSet 对应于数据库中的现有表,并且您的实体中的所有属性都对应于这些表中的现有列。

    如果你检查了所有这些,它仍然失败。然后从 MSDN code first to an existing database阅读这篇文章

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2017-07-03
      • 2014-02-28
      • 2014-03-15
      相关资源
      最近更新 更多