【问题标题】:.Net Core Reverse-Engineering with database DNS connection.Net Core 逆向工程与数据库 DNS 连接
【发布时间】:2016-10-10 21:11:42
【问题描述】:

我的问题相当简单,我已经做了很多寻找答案的工作,但未能找到解决方案。但是,这似乎并不罕见,因此,如果我忽略了一些简单的事情,或者有一个我忽略的参考资料可以解决我的问题,我将不胜感激!来了……

尝试通过我的新 .Net Core 项目连接到现有的异地数据库。我已按照此处的逆向工程说明进行操作:https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

这些说明适用于我本地计算机上的数据库,但是当我使用连接字符串到异地服务器的数据库 DNS 运行 Scaffold-DbContext 时,它会创建上下文但没有实体。所以我的上下文类文件如下所示:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace CensusApi.Models
{
    public partial class CensusDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
        optionsBuilder.UseSqlServer(@"Server=database.dns.org,[port];Database=mydatabase;Integrated Security=False;User ID=myuser;Password=mypassword;");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }

    // Unable to generate entity type for table 'dbo.LANDING_DEMOGRAPHICS_2010'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.LANDING_ECONOMIC_2007_2011'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.LANDING_STATE_FIPS'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.LANDING_ZIP_STATE'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.SAMAIN_ZIP_STATE'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_HOUSEHOLDS'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_HOUSING_OCCUPANCY'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_MEDIAN_AGE'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_MEDIAN_INCOME'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_POPULATION'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_POPULATION_BY_RANGE'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_RACE'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_RACE_HISPANIC_AND_LATINO'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_RELATIONSHIP'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.STAGE_ZIP_STATE'. Please see the warning messages.
    // Unable to generate entity type for table 'dbo.LogShipStatus'. Please see the warning messages.
    }
}

ErrorList 窗口指的是上面代码第 11 行的#warning(即保护连接字符串中潜在敏感信息的警告)。我意识到做这个逆向工程之后的下一步是重新定位连接字符串,而#warning 似乎是一个通用警告,而不是对逆向工程过程的阻碍。

我尝试过的凭据包括 sa 用户和受限用户。两者的结果相同,因此这似乎不是权限问题。

我应该使用不同的方法或框架连接到外部服务器吗?

任何想法或反馈将不胜感激!

【问题讨论】:

  • 感谢您花时间回复,Tseng。但是,移动连接字符串并不能解决我的问题,因为 Scaffold-DbContext 命令需要连接字符串。一旦连接字符串被移动到 Startup.cs,有没有办法可以引用它来重新脚手架,或者有没有办法使用修改后的上下文来更新脚手架?
  • 您的上下文是否与 Web 项目在同一个应用程序中?当前存在一个限制,即命令行工具仅在上下文位于应用程序中时才起作用,而在类库中不起作用。尽管docs.efproject.net/en/latest/miscellaneous/cli/… 有一个解决方法
  • 您成功生成实体的数据库是否与您尝试通过 DSN 连接的数据库相同(模式方式)?

标签: c# asp.net-core asp.net-core-mvc entity-framework-core .net-core


【解决方案1】:

确保您的表具有主键。

我在我的数据库中只有一个表时收到了这个确切的错误消息,我意识到我忘记了主键,并且在我再次运行 DbScaffold 命令后它运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 2017-05-05
    • 2023-03-24
    • 1970-01-01
    • 2011-12-21
    • 2016-01-20
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多