What to put in start_application() to deploy to AppHarbor? - asp.net

I am trying trying to deploy a very simple asp.net MVC5 application to AppHarbor and the any page that requires a DB query produces the following error:
Invalid object name 'dbo.AspNetUsers'.
Referencing what this guy has to say:
https://www.youtube.com/watch?v=XCXWVHwHVqk
I added the following to my start_appliation() method in the Global.asax.cs file:
Database.SetInitializer<ApplicationDbContext>(
new CreateDatabaseIfNotExists<ApplicationDbContext>());
This rewarded me with a new 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.
This is the connection string in my web.config file:
In the AppHarbor dashboard I set the connection string alias to "DefaultConnection" as well.
What on earth should I be putting in the start_application() method? Or is there something else completely screwed up?

Related

Entity Framework Reverse POCO

We have an existing Db with about 100 odd tables - These were created the old way - use sql queries to generate the Db and use the SQL reader/writer to access and update the DB's.( ADO.net). A developer prior to me added few new tables to this using the EntityFramework (EDMX) approach. They just named the tables generated newly "DBEntities" and used this as a data source for the new pages that were then written in ASP. net webforms(apsx).
I am now tasked with developing some of the newer pages on this existing webforms app to be built in Angular 2.0. I started off with consolidating the one DB that this app reads ( which were referencing both the EDMX file and ado.net - having two config file entries - add name ="DbConn" connection string "".. and add name="DBEntities" the edmx conn string".) I removed the edmx files , used the reverse poco generator and created first class POCO's and generated my tt file. Updated my web config to include only one connection string "DBConn". Tested the app - all the old web form pages were working well. Only those aspx pages(controls that use DataBind..) that were referencing the EDMX files gave me this error - The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
I have tried everything(regenrated POCO's.. checked all the table names against the aspx data sources everything looks good..) and nothing seems to work. Using EF6. - Any help is appreciated..
Thanks much.

Entity Framework 5, Migrations, MVC 4, update-database -force - best way to run on server

Visual Studio MVC 4 web app, SQL Server 2008 R2, Entity Framework 5
I have automatic migrations enabled (in configuration.cs):
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = false;
}
As a precaution, DataLossAllowed is disabled. What's the best way to run this migration manually, locally and on the server?
I get this error:
The "WebSecurity.InitializeDatabaseConnection" method can be called only once.
when I try to run it from package-manager.
It seems like manually running migrations, and making them automatic are incompatible?!
That error looks like you are initializing your database connection more than once rather than having an issue with Migrations.
WebSecurity.InitializeDatabaseConnection is what is called when you use the [InitializeSimpleMembership] attribute, which in MVC4 decorates the Accounts controller by default.
As you only want this to be called once, you should remove the attribute from your Account Controller and add a call to WebSecurity.InitializeDatabaseConnection to AuthConfig file, which is called on application start by global.asax. You could put it straight into the application start method in global.asax or make your own file but AuthConfig seems the best place for it in my opinion.
The code for it looks like this
WebSecurity.InitializeDatabaseConnection("DatabaseConnectionString","UserProfile","UserId","UserName", false);
renaming the DatabaseConnectionString to the name of your connection string in web.config
Also the false may be True if you are not using Db First.
Once you do that, you can remove the InitializeSimpleMembership file from the filters folder too.
This will ensure that InitializeSimpleMembership is only called once.

There is already an object named 'Applications' in the database

I got this error in my asp.net webapi service when I call method Membership.ValidateUser(email, password);.
I am using EF 4.3.1. DB created using this code
using (var db = DbFactory.Get())
{
Console.WriteLine("Creating new database...");
db.Database.Create();
Console.WriteLine("Initializing with initial objects...");
db.Database.Initialize(true);
}
I get this error only once on first start of my service.
Does anybody know why it happens?
It sounds like you already have an object named 'Applications' in the database the first time your application starts.
Does the code snippet you provided only run at startup?
Try dropping the object through the database management engine, and then re-running your service.
The error is self-explanatory.
You are trying to create an object (ex: table, sp, view, etc.) that is named Applications but that object already exists in the database.
Maybe you didn't remove it after initial testing.
Providing you're trying to run it on the right project:
Check your migrations folder, the Up method of your InitialCreate class is probably trying to create the Application table. You can either
Drop the table and run the code again
or
Set up your InitialCreate properly to handle this scenario

LinqToSQl changing Datasource

I am working on a web application project where the DAL is generated using LinqToSql designer in Visual Studio. All data access logic is in a separate project.
I created a copy of the database with same structure and same names for tables, views etc. I tried to change the connection string to the new source, but it is not working. Somehow, it still accesses the old database.
All of the following changes failed:
Changed the connection string in config files for all the projects in the solution which is now pointing to new data source.
Create a partial class for the DataContext and passed new connection string to the constructor after changing the connection string to "none" in Visual Studio LinqToSql designer properties.
Passed the connection string explicitly whenever I instantiated the DataContext.
Deleted the contents of bin and obj folders for the LinqToSql project and rebuilt everything.
My project is saving new data to the new datasource, but when reading data it is getting it from the old database, and also throwing null exceptions when it connects to the old DB for data.
What am I doing wrong? Is there any fix for this? What is the best way to make a project with LinqToSql DataContext point to a new datasource with the same data (for example: dev, staging, release, etc.)? Thanks.
If you are reading data from the old database, there must be a connect string or similar in your project that hasn't been updated.
There are three places where the LINQ to SQL designer might be storing the old connection information:
In the app.config file.
In the Settings.settings file.
In the default constructor for the DataContext.
Check all of them.
You could also use 'Find' to search your entire solution for any other references to the old database hard-coded into your application.

MyGeneration.dOOdads problem with MySQL connection in ASP.net with stored procedures

I am using MyGeneration.dOOdads which helps in generating Business layer and Data layer easily, but currently I am facing a problem with it.
I had worked with MS SQL database and was successful, but now I have to work with MySQL 5.
I have the generated data layer classes of MySQL database successfully, but when I add them in my project along with the reference of MyGeneration.dOOdads dll file it gives error of no MySql4Entity class in the namespace.
I have generated the class according to the given instructions and also added the reference of mysql.data in the project but still this error is occuring.
Please guide me if you have used MyGeneration.doodads for MYSQL5 database in asp.net.
Also please tell me how to make stored procedure using MyGeneration.doodads of MYSQL database?
open doodads project (source code) .. right click on MySql4Entity class (and other classes related to mysql) and choose 'properties' the choose build action as 'compile' from the list ... to compile the file then build the project and it its output as reference to your project ...

Resources