How to handle missing ASP Membership Database for Deployment/Checkout? - asp.net-mvc-3

I have an ASP MVC 3 application using SQL Server 2012 LocalDB. I haven't been commiting the membership database aspnetdb.mdf to source control (Mercurial) so when I checkout or deploy the project to a new computer I get the following error:
Could not find stored procedure 'dbo.aspnet_CheckSchemaVersion'.
I would prefer not to run aspnet_regsql on every machine. Is there a way I can re-create the missing membership database from code in Global.asax? Failing that, is it safe to commit the initial database (either .mdf or script to .sql) created by aspnet_regsql into source control and deploy it to all machines?

It doesn't seem to be possible to create it from within .NET, so I ended up just creating the default membership database:
"%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regsql.exe" -E -S (localdb)\v11.0 -d aspnetdb -A mr
This will create aspnetdb.mdf in C:\Users\USERNAME. I then copied that to a deployment folder in my solution and added it to version control. The last step was to add a build event to my web project which will copy the database over if it does not exist.
if not exist ("$(ProjectDir)App_Data\aspnetdb.mdf") copy "$(SolutionDir)_Deployment\aspnetdb.mdf" "$(ProjectDir)App_Data"

Related

How to use archiveDir with Web Deploy 3

I am currenlty using Web Deploy 3 with Automatic Backup. Yesterday, backup failed with error message 'ERROR_PACKAGE_TOO_LARGE'.
as per MSDN
Resolution - Use the archiveDir provider when creating a package instead. Currently there is no solution for this limit with respect to automatic backups.
Now archiveDir works fine on command line
msdeploy -verb:sync -source:apphostconfig="Site" -dest:archivedir=c:\archive
I am pretty new to Web Deploy, can someone please help me to automate this with Web Deploy using VS2013? I don't want to login to server to manually back up my site and then come back to VS to publish. Is there any setting in .pubxml(Publish Profile) to achieve it?
Thanks

How to create a user for a login in 2013 SQL Server Database Project

I'm trying to create a user with a login for a SSDT database project. The login already exists on the target server. With the following SQL:
CREATE USER [MyLogin]
FOR LOGIN [MyLogin]
WITH DEFAULT_SCHEMA = dbo
GO
I get the error:
Error 1 SQL71501: User: [MyLogin] has an unresolved reference to Login [MyLogin].
I've found two solutions, but neither work with 2013:
1) Create a server project to reference the login. This isn't an option in the current version of SSDT / VS2013
2) Disable schema checking. The option is missing in 2013 (I believe it was Options -> Database Tools -> Schema Compare)
You can actually create the Server Level Login within the SQL Server Database Project using the standard T-SQL Syntax:
CREATE LOGIN [Login_Name] WITH PASSWORD = '<Password>';
Note: You can also right-click the database project and choose 'Add New Item' and navigate to SQL Server > Security (within the templates dialog) and select 'Login (SQL Server)'.
This resolves the SQL71501 Error and (assuming you are using SQLPackage.exe for deployment) will allow SQLPackage.exe the ability to compare the security object with the target Database before deployment and publishing occurs.
Hope that helps :)
If you are guaranteed to have the login on the server, your best bet is to tweak your master dacpac file. There are some instructions on how to do this here:
http://sqlproj.com/index.php/2013/02/how-to-add-objects-to-master-dacpac
Alternatively, you could remove references to logins from the SSDT portion and handle it in post-deploy scripts. If you have any sort of environment where different permissions need to be applied in different server (Development, QA, Production), that might be the better option. I've blogged about this here: http://schottsql.blogspot.com/2013/05/ssdt-setting-different-permissions-per.html
Hopefully one of those will help. I've actually used the first option to work around an issue with needing to use EXECUTE AS, which requires that the user is in the project. It was a little tricky to get the exact XML, but I worked around it by creating an empty project with just that login, building it, and copying the XML from the dacpac into the master dacpac.

Dev->Stage->Prod with Git deployment for Azure Websites

How best should I accomplish the following deployment objectives with Git deployment for Azure?
Easily switch when working locally to either use fake in-memory data or (eventually) non-production snapshot of real data
Deploy to staging environment on Azure such that at first I could use fake in-memory data and eventually move to non-production snapshot of real data.
Deploy to production with real data
I currently deploy using Github and a staging branch to a staging Azure website. Since I deploy to a public repo, the web.config file is ignored by git. (EDIT: I just learned that ignoring web.config actually causes deployment error on azure)
Any help/suggestion is appreciated.
It's actually supposed to be simpler than that. Please see this page. Basically, the idea is that you set some AppSettings in the Azure portal to override the default values that are committed to your repo.
Well... Here's what I did that works for me right now.
To quickly switch between fake in-memory data locally, I use a compilation symbol LOCAL and a preprocessor directive #if LOCAL.
Same compilation symbol works when you deploy to Azure, so I can work on fake data until I'm ready to switch to real db. I can also use the app settings if I really want to make to switch it more easily.
The challenge was to keep a web.config with "secrets" (like connection string) locally and not expose it to Github. I added it to .gitignore, but then my deployments started failing on Azure because it could not find the web.config. Just copying it to wwwroot via ftp did not help - Azure was looking for web.config in the repository.
So, to make this work I "slightly" altered the deployment process by first copying the Web.config from wwwroot to the repository before running the default deploy.cmd. This was simple - this is what you do:
Create a .deployment file in the root of your repository with the following:
[config]
command = deploy.my.cmd
Create deploy.my.cmd with the following script:
xcopy %DEPLOYMENT_TARGET%\Web.config %DEPLOYMENT_SOURCE%\\ /Y
deploy.cmd
Now, I have web.config with secrets locally. Git ignores this file. I uploaded the correct web.config to Azure via FTP, and it gets used whenever I deploy.

MVC4 Membership Tables not created on production environment

I'm attempting to use the ASP.NET universal providers in my MVC4 application. Among other places, this article describes how, the first time you attempt to register a user, the tables should auto-create in my DB.
I had this working on my internal app/db, but I used the asp.net web configuration tool to create my first user. When I deployed this to production, I attempted to register a user and was hit with an error indicating dbo.Applications didn't exist, which is one of the tables that should have been created.
Any thoughts on why these tables weren't created? I've made sure the System.Web.Providers dll was copied to the bin folder. I'm also sure my app can manage CRUD tasks on my database.
argh duh. Turns out I didn't have create perms set up for the app pool identity that was making the call. Hangs head in shame

How to deploy SqlCe Database into isolatedStorage for windows phone

say, I want to use an existing SqlCe Database which HAS data in it. I am not sure which is the best way to do it.
1) here is the normal way to create a SqlCe Database for Windows phone
using (CountryDataContext context = new CountryDataContext(ConnectionString))
{
if (!context.DatabaseExists())
{
// create database if it does not exist
context.CreateDatabase();
}
}
But this Database has data in it , so I dont want to create it. I want to deploy it or store in in isolatedStorage.
How should I do it ?? I want to use the data already in the database.
Thanks
You can include the database file as Content in your application project and it will then be put in your XAP at build time and copied to your application install directory on the phone.
Will you modify this database at all?
If not: you can access the database directly from the install directory and future updates will get any new version of the database automatically (as long as you remember to add them to the XAP).
If you do modify it: then on first install you will need to copy the database to isolated storage before you use it. You will need to watch out if you update the database scheme with a future update.
Usually the pattern is to create the database and to configure seed data with it.
Alternatively, if the database is read-only, you can deploy it with your application. See How to: Deploy a Reference Database with a Windows Phone Application on MSDN.
If you want to deploy a database from your development machine for testing purposes, you can use the the ISETool that can take and restore snapshots of an application's isolated storage to/from a local directory:
# Copy data from IS to directory
ISETool.exe ts xd <PRODUCT-ID> "C:\TempDirectory\IsolatedStore"
# Copy data to IS from directory
ISETool.exe rs xd <PRODUCT-ID> "C:\TempDirectory\IsolatedStore"

Resources