我的问题很简单,我已经做了很多寻找答案,但找不到解决办法。然而,这似乎并不是一个不常见的场景,所以如果我忽略了一些简单的东西,或者有一个我忽略的参考来解决我的问题,我会感激一些指导!这就是......
尝试通过我的新.Net Core项目连接到现有的非现场数据库。我在这里按照逆向工程的说明进行操作:https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html
这些说明适用于本地计算机上的数据库,但是当我使用连接字符串将Scaffold-DbContext运行到非现场服务器的数据库DNS时,它会创建上下文但不创建实体。所以我的上下文类文件如下所示:
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用户和受限用户。两者都具有相同的结果,因此它似乎不是权限问题。
我应该使用不同的方法或框架来连接外部服务器吗?
非常感谢任何想法或反馈!
答案 0 :(得分:22)
确保您的表有主键。
我收到了这个确切的错误消息,我的数据库中只有一个表,意识到我忘记了主键,并且在我再次运行DbScaffold命令之后运行正常。