Creating a development copy of MVC project - asp.net-mvc

I've created an ADO.NET, MVC4, EF5 project which has a relationship between an entity and the built in Membership provider's User.
I have cloned (using git) my repository to an external server and have set up a database with the same name and run the .edmx.sql file created by ADO.NET.
When I run my site on the external server I get the following exception: "Invalid column name 'User_UserId'."
Looking in the database, I can see that Users, Roles, etc. tables have not been created.
What is the best way to do this kind of deployment and rectify my woes?
Update
So I added the tables in question (Users, Roles, UserRole, Applications) to the database by scripting it from the database on my local machine and still the same error occurs.
This seems to be a problem with the mappings in System.Web.Security.Membership.
Any help please?

Okay! Managed to do it. So for anyone else with the same problem - try this:
Create a new DB on the external server
Run the .edmx.sql file on that DB
Change the connection string to your server's name
Run the site locally - this will create the Users, Roles, etc. tables

Related

Database is not getting created at first time

How to re-create the database using EF6?
I have tried both of following post but, i don't know why its not working and getting same error.
How do I generate an EF6 database with migrations enabled, without using update-database?
Migrations is enabled for context ''but the database does not exist or contains no mapped tables
I have already published my sample on the web server. I am using Sql Server 2012 Express DBMS.
When i have created my application and published it on web server it was working fine. It has created database automatically. After that i have made some changes in ApplicationDBContext and added some properties in IdentityModes(ApplicationUser). Then I have migrated database on my Local IIS Express and it was working fine. Then I have tried to publish my sample again on web server but, this time it shows an error.
Migrations is enabled for context 'ApplicationDbContext' but the database does
not exist or contains no mapped tables. Use Migrations to create the database
and its tables, for example by running the 'Update-Database' command from the
Package Manager Console.
I have deleted the database on web server and tried again. but, still i am facing same error. Now, there is an empty database without table.
Application is creating database on User Registration.
I have also read this post and tried to call dbContext.Database.Initialize(true); and ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabase() method but, still its not working.
First, automatic migrations are nice for development but it's a hugely bad idea to rely on them in production. Second, at some point you turned off automatic migrations, which is why your production database is no longer having tables created in it.
Go ahead and leave automatic migrations off; even in development it's better to know exactly what changes you're making to your database by generating a migration, seeing the code that will be executed, and only then running Update-Database to apply those changes.
You have a number of choices in terms of getting schema updates into production, but they all boil down to roughly the same procedure.
Generate a new migration against your local database.
If you want to update your production database via SQL, run Update-Database -Script. This will generate a SQL file with the code to run on the database to migrate the schema. Hand this off to your DBA if you have one or review the SQL code yourself and then apply it manually to your database via Management Studio.
Run Update-Database. This time without -Script to make the changes to your local database schema.
If you didn't generate the SQL file in step 2, view your local database in SQL Server Object Explorer in Visual Studio. Right-click on the local database there and choose, "Schema Compare...". Follow the wizard to compare with your production database. In the end, you can either generate a SQL file with the necessary changes that need to be made (which again, you'd hand off to your DBA if you have one), or you can migrate to production directly from Visual Studio. However, this is not really recommended. It's always best to generate the SQL, which you or your DBA can then inspect before applying the changes live.
Chris Pratt is correct BTW.
I had downloaded a project in which I needed to just run "Update-Database
Here are the quick screenshots
Then at the bottom of Visual Studio
PM> Update-Database
DONE

Changing connection string from dropdown

I created a basic MVC app to do CRUD operations on database tables. I'm using Entity Framework Database First, so I was able to just scaffold out controllers and views very quickly.
We have a dev, qa, and prod database. The schema should be identical with only the data in each being different.
I want to create a dropdown that will let me change the connection string from the app. I'm not sure the best way to do this. It currently works just fine using the dev connection string, but if I manually change the database to qa from web.config, whenever I try to create or update an existing record from the app, I get this error:
"The UPDATE permission was denied on the object"
I don't get this error using the dev database.
The problem is with your database permissions.
Use the following link and go through the security/permissions and you will be able to solved the problem.
http://blog.sqlauthority.com/2012/04/23/sql-server-introduction-to-sql-server-security-a-primer/
Please verify that your user has the same permissions on qa/prod as it does in dev.

Entity Framework creating settings table that I can't put on Azure

I've been trying to create an ASP.net - MVC app that uses entity framework to connect to an Azure database.
I used a database first method to try and create the Model, I was able to connect to my azure database and successfully create the model.
Everything was working perfectly and I was able to retrieve my values from my azure database then I tried to publish the website to an Azure website. The website works fine and I have the website registered as able to use the azure database. When I click on the button that hits the controller to access the database I then get "Sorry, an error occurred while processing your request."
When I looked into this a bit more I seen that when I created my Entity Data model using the wizard it created a connection string to a local db for my entity connection settings. I'm guessing this is saving some entity framework settings but my problem is obviously when I publish the website to Azure it can't access this local db.
Any suggestions on how to either get this DB onto Azure or avoid using this extra database?
Thanks
This is the best tool to migrate your local database to Azure. Before publishing your site change the web.config to point to the Azure database. It will solve your problem.
Like you already explained, it seems that you web.config still points to your local database. Try to look at your web.config and see if you have connection strings pointing to .\SQLExpress, localhost\SQLExpress, ".", ...
Then, like Geenthanga's explains, you need to modify this value before deploying (it should point to your SQL Azure database instead). But you shouldn't do this manually! Instead, consider using web.config transformations, as explained here: Web Deployment: Web.Config Transformation.
This way you can have 2 web.config transformation files:
Web.Debug.config: This file would contain the transformations to add the connection string pointing to your local database.
Web.Release.config: This file would contain the transformations to add the connection string pointing to SQL Azure.

Asp.net membership provider with locally attached DB

I've created a locally attached SQL Server 2008 Express for an ASP.NET MVC application (in App_Data), but I need to access the built-in users table for relationships to custom models that I'm creating. Since the database is local, it doesn't show up when I run aspnet_regsql.exe. Is there a way around this, or should I be setting up the database as a true SQL Server database and providing scripts to generate the tables?
I'm trying to get an environment configured so anyone who checks out the code will have the database automatically, instead of having to spend time configuring it before they can write any code (basically a situation like Rails has where you can have somebody new check out the code, and maybe with some minor configuration type rake db:create and rake db:migrate and have the entire working environment set up.
I'd recommend using a SQL Server database and you can then use aspnet_regsql.exe to generate the schema for you.
If the membership provider is working make sure you have selected 'Show all files' in solution explorer and check the App_Data folder. The DB will probably be called something like ASPNETDB.MDF. ASP.NET / VS has a habbit of setting this database up for you and trying to hide it.

How can I create a local user profile for the anonymous user of an ASP.Net MVC application under IIS 7?

I've been experimenting with ASP.Net MVC, and have come across a problem that is probably not specifically MVC related. But I cannot get the authentication in the default MVC application (the one created by the wizard when you create a new MVC project) to work properly under IIS 7 on Windows 7.
If I run under the Visual Studio environment, it works, but if I switch the settings to run under IIS instead, I get the following exception trying to submit the login or registration:
Failed to generate a user instance of
SQL Server due to failure in
retrieving the user's local
application data path. Please make
sure the user has a local user profile
on the computer. The connection will
be closed.
I believe that this is because the website runs under my own account in Visual Studio, but under the IUSR account in IIS. Google searches on the exception message have been unhelpful so far.
So, can one create a local user profile for the IUSR account? If so, how? Is there something else I should be doing to get the SQLExpress engine to work under the anonymous account in IIS 7?
I also tried configuring the IIS website to use my account, but since this is my home machine, my account doesn't have a password, and it appears that IIS won't let a website be configured to use an account without a password. Or, since this is my first experience with IIS 7, and configuration feels very different than IIS 5/6, I am just missing the right setting that will let me configure the account to use for anonymous access.
EDIT: Some additional information. If I empty the App_Data folder and try again from IIS, SQLExpress attempts to create my database and fails, but the exception message has further information with the following suggestions.
SQLExpress database file auto-creation
error:
The connection string specifies a
local Sql Server Express instance
using a database location within the
applications App_Data directory. The
provider attempted to automatically
create the application services
database because the provider
determined that the database does not
exist. The following configuration
requirements are necessary to
successfully check for existence of
the application services database and
automatically create the application
services database:
If the applications App_Data directory does not already exist, the
web server account must have read and
write access to the applications
directory. This is necessary because
the web server account will
automatically create the App_Data
directory if it does not already
exist.
If the applications App_Data directory already exists, the web
server account only requires read and
write access to the applications
App_Data directory. This is necessary
because the web server account will
attempt to verify that the Sql Server
Express database already exists within
the applications App_Data directory.
Revoking read access on the App_Data
directory from the web server account
will prevent the provider from
correctly determining if the Sql
Server Express database already
exists. This will cause an error when
the provider attempts to create a
duplicate of an already existing
database. Write access is required
because the web server accounts
credentials are used when creating the
new database.
Sql Server Express must be installed on the machine.
The process identity for the web server account must have a local user
profile. See the readme document for
details on how to create a local user
profile for both machine and domain
accounts.
I've pretty extensively confirmed that the first three suggestions have been satisfied. The fourth seems to be the cause of my problems, but I can't figure out how to do that. And although the suggestion claims there is a readme document that describes it, I have not been able to find that document.
I got this problem as well running under medium trust. The process that creates the database requires at least High trust. You can check this by looking in your Web.Config for
<trust level="TrustLevel" />
If there is no trust specified in your Web.Config, try adding it and set it to either Full or High. If this doesn't work, there is a machine.config in your IIS which you would need to modify.
That being said, the best route I have found to solve this is to just use aspnet_regsql.exe to create the necessary tables and then change the connection string in your Web.Config to look at it directly.
I solved "The directory 'LocalApplicationData' does not exist." error when running an SSIS package through an SQL Job by ensuring the SQL SSIS service and SQL Server Agent service were running under the same account as the swql job was set to use!
This in my case was a domain account.
Solution: Try UNINSTALLING any updates before you started experiencing the issue. I spent countless hours - wasted hours that I will never get back in my life - reading and following every solution possible without success. I uninstalled all SQL Server updates and now everything works fine.

Resources