my question is fairly simple and I've done a lot of searching for an answer but have been unsuccessful in finding a solution. This doesn't seem like an uncommon scenario, however, so if I'm overlooking something simple or there's a reference out there that I've overlooked that addresses my issue, I'd be grateful for some guidance! Here goes...
In trying to connect to an existing, offsite database through my new .Net Core project. I've followed the instructions for reverse engineering here: https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html
These instructions work for a db on my local machine, but when I run Scaffold-DbContext with a connection string to a database DNS for an offsite server, it creates the context but no entities. So my context class file looks like this:
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.
}
}
The ErrorList window refers to the #warning on Line 11 of the code above (i.e. the warning to protect potentially sensitive information in the connection string). I realize the next step after doing this reverse engineering is to re-locate the connection string, and that #warning appears to be a generic warning and not a hinderance to the reverse engineering process.
The credentials I've tried have included the sa user as well as a restricted user. Both have the same results, so it doesn't appear to be a permissions issue.
Is there a different method or framework I should use for connecting to an outside server?
Any ideas or feedback would be greatly appreciated!
Make sure your tables have primary keys.
I was getting this exact error message with only one table in my database, realized I had forgot the primary key and after I ran the DbScaffold command again it ran fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With