使用数据库DNS连接进行.Net Core反向工程

4
我的问题很简单,我已经搜索了很多答案但是没有找到解决方案。然而,这似乎并不罕见,所以如果我忽略了一些简单的东西或者有一个我忽略了的参考可以解决我的问题,我将非常感激您的指导!以下是我的问题:
在尝试通过我的新.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.
    }
}

错误列表窗口指的是上面代码中第11行的#warning(即保护连接字符串中有潜在敏感信息的警告)。我意识到在进行逆向工程之后的下一步是重新定位连接字符串,而#warning似乎是一种通用警告而不是对逆向工程过程的阻碍。
我尝试使用的凭据包括sa用户和受限制的用户。两者都有相同的结果,所以这似乎不是权限问题。
对于连接到外部服务器,是否有不同的方法或框架可供使用?
如有任何想法或反馈,非常感谢!

感谢您抽出时间回复,Tseng。然而,移动连接字符串并不能解决我的问题,因为Scaffold-DbContext命令需要一个连接字符串。一旦连接字符串被移动到Startup.cs中,是否有一种方法可以引用它来重新生成OR是否有一种方法可以使用修订后的上下文更新脚手架? - kajoseph
你的上下文是否与Web项目在同一个应用程序中?目前有一个限制,即命令行工具仅在上下文位于应用程序中时才能正常工作,而在类库中无法工作。不过,有一个解决方法 https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html#targeting-class-library-projects-is-not-supported - Tseng
你成功生成实体的数据库与你尝试通过DSN连接的数据库(从模式上)是否相同? - Mike Brind
1个回答

22

确保您的表具有主键。

我在我的数据库中只有一个表格时收到了这个错误信息,后来意识到我忘记了主键,在我再次运行DbScaffold命令后,它正常运行了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接