Wednesday, April 27, 2016

System.InvalidOperationException: Migrations is enabled for context but the database does not exist or contains no mapped tables.

Problem:
Entity Framework throws migration exception when trying to move code to Staging Server.


I have been trying out the EF migration and Code First approach on a new project.
I was able to get the migration and update working on my dev box.  I then went to move
the code base to our staging using xcopy.   I then hit the website and got a nasty no mapped
tables error.


Solution:


For me I don't need to move the migrations to the staging or production server.  I just needed to disable the migration.  This can be done using the web.config:




This is the basic syntax for setting an initializer in your config file:
<appSettings>
< add key=”DatabaseInitializerForType MyAssemblyQualifiedContextType”
value=”MyAssemblyQualifedInitializerType” />
< /appSettings>

The key things to notice here are:
  • The entry goes in the appSettings section of your config file
  • The key must always start with the string “DatabaseInitializerForType” followed by whitespace
  • The second part of the key is the assembly-qualified name of the context type for which the initializer should be set
  • The value is the assembly-qualified name of the initializer to set, which must expose a public, parameterless constructor so that EF can create an instance of it
Example:


<add key="DatabaseInitializerForType FCJC.Models.FCJCContext"
value="Disabled"/>


I placed this in my web.staging.config so that each time I build out my solution for deployment to staging, it will insert this flag and no more error!






Source:
https://blog.oneunicorn.com/2011/03/31/configuring-database-initializers-in-a-config-file/

Thursday, April 7, 2016

System.DirectoryServices.DirectoryServicesCOMException: A local error has occurred.

Problem:

Running an MVC 5 based solution which is using AD on prem for ID control.  This solution depends on the UserPrincipal call to get a user.  The error was occurring in code on this line

UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, windowsaccountname);

It worked fine yesterday but just stopped working.

Solution:

Step 1:

Checked with our Admin staff and confirmed that a GPO had been applied against all accounts to include service accounts.  The IIS app pool is using a service account to connect to the database.
The policy turned on Smart card required property for the service account

Smart Card option active







The obvious issue is that a service account is not going to use a smart card.  So solution was to disable the smart card action and restart application pool.

Remove Smart card requirement

Step 2:

Now getting this error!

 Not to worry this can be resolved by resetting the password in Active Directory

 
Change Password

 Step 3:

Now go to IIS and reset password on application pool for the web application

Add user name and password you changed in Step 2.

 Done!

Source:

http://fetchmytip.blogspot.com/2016/02/cac-piv-adfs-ldap-oh-my.html

Tuesday, April 5, 2016

MVC 5 bootstrap template not running under IIS express 7 -- Take 2

Problem:

Posted this problem earlier and gave a solution which seemed to work. 


"I have been running MVC project using default bootstrap template.  It works fine when I deploy it to our staging server.  When I attempt to run it locally on my desktop none of the bootstrap files are working.  I get no branding.


I wracked my brain on this and thought it might be missing files.  Then hit f12 on the browser and saw several of these gems:"






My original solution did not get it right

Appears that the css and js files are being blocked in iis express.  I am running this site using SSL and ADFS trust.  This appears to be causing some issues with local file access.  I know that IIS express runs as the local user.  I still granted IUSR and IIS_USRS accounts full access to the folders where the site was running on my local box.  This appears to have resolved the issue.

The real issue is NOT IUSR access to the local project folders.  Stack overflow gave me a push in the right direction.  The line: "I have Anonymous Authentication and Forms Authentication disabled. ASP.NET Impersonation and Windows Authentication are both enabled." helped me recognize that in IIS Express I might need to check the solution properties.

Solution:

Closer inspection of the properties see image 1 shows that the Windows Auth is set ot disabled. 




Image 1


My particular application is using SSL and PKI authentication.  The project needs to enable windows auth for my images and scripts to be available to the application in IIS Express.  Image 2 shows the Windows Authentication


Image 2
Once you set the Windows Auth to Enabled this will grant permissions to images, scripts and styles.




Source:

  1. http://fetchmytip.blogspot.com/2016/03/systemsecuritycryptographycryptographic.html
  2. http://stackoverflow.com/questions/16902601/http-error-401-2-unauthorized-for-new-site-in-a-working-app-pool