1

Just for a brief overview this is how I added the database into my project:

  • I have added a datasource by adding an ADO.NET Data Model Entity and selecting EF Designer from Database.

  • Doing so has generated a connection string for me in my web.config. Integrated Security is set to true (if that matters).

  • Once connected I right clicked and selected 'Update model from database.

Since there is multiple environments I built a custom context with a parametized constructor. Code looks as such (condensed and censored):

 public partial class DataEntities : DbContext
    {
        public DataEntities(string connectionString)
            : base(connectionString)
        {
        }
    }

With each environment there are different local sql accounts associated. Requiring me to generate a connection string that is associated with the correct account in the correct environment. Doing so my web config looks something like this:

  <connectionStrings>
    <add name="DataEntities" connectionString="metadata=res://*/Models.XXModel.XXModel.csdl|res://*/Models.XXModel.XXModel.ssdl|res://*/Models.XXModel.XXModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MYSQLSERVER;initial catalog=Data;integrated security=True;MultipleActiveResultSets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <secureConnectionStringsSection passwordPolicy="AllowLocalPasswordsForConnectionStrings">
    <secureConnectionStrings>
      <add name="DataEntities-Local" providerName="System.Data.SqlClient" connectionString="data source=MYSQLSERVER;user id=ACCNT; password=PASSWORD;" />
    </secureConnectionStrings>
  </secureConnectionStringsSection>

When I am instantiating my DataEntity object, I am calling a helper function I have written to get my custom connection string. I am doing so via the following code:

private DataEntities adDB = new DataEntities(XXX.Helpers.EFDBHelper.getDataConnectionString());

I can verify that my connection string helper does correctly pull the custom connection string that I have in my web.config. However once I actually try to make a call on the database I am left with the following error:

Invalid object name 'dbo.mycolumn'.

I know that my parametized constructor is being called with the correct connection string. I also know my connection string is valid, I have tested it in powershell:

$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "data source=MYSQLSERVER;user id=ACCNT;password=PASSWORD"
$conn.Open()
$conn.Close()

I am certain that it is not an issue with the database itself as well. The moment I remove the parameter from my Entity initialization:

private DataEntities adDB = new DataEntities();

I am able to pull data from the database. I am assuming that it uses the auto-generated connection string. Which won't work since I cannot use integrated security once it goes past my local environment.

My apologies, I am new to the technology here. I am sure that it is just something small that I am missing.

1 Answer 1

0

In my connection string I did not specify a catalog.

Since I am newish to the technology, I tried to simply replicate some of the existing code that I had inherited. In the connection string I tried to replicate from, no catalog was expressed as well.

However... the account I am using to talk to the new datasource has access to all levels of the database so the catalog was required. The old inherited database had only one catalog the the sql account had access too.

You live and you learn!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.