I have been working through various tutorials and their links to instructions, to make and deploy code first web applications with databases.
The tutorial I followed for the application was the Movie tutorial. I used this tutorial for my own purposes (I have kept it simple at the moment) and I have the application working.
I came to trying to deploy (I use 1 click) and the first time I tried it failed as I hadn't configued the database side of things at all. So I followed a lot of instructions and I eventually got the site to deploy.
The first time I did this I checked the target server for the database (which is different to the application) and I couldn't see the database. The result was that when I went on to the application the site had caught the error on the sites general error.aspx
that the MVC framework produces as standard.
I looked at little further on the SQL server and noticed it had added the database in the system databases, so I deleted them from here and created a new database called "Outages".
I then changed the various bits of code to reflect this and deployed again, this time the tables were created as expected, however the site still was not working from the live server.
I'm not sure where I am going wrong or how I can trace the error as it is just getting caught on the standard error page.
All the account pages work fine it is just the data related pages that produce the error.
I have a feeling it is something to do with this, I had the same problem once with an account model that I had customised and it was where I tried to reference something in the controller that was creating an error.
public ActionResult Details(int id = 0)
{
Outage outage = db.Outages.Find(id);
if (outage == null)
{
return HttpNotFound();
}
return View(outage);
}
So I have a feeling it is failing on the line: Outage outage = db.Outages.Find(id);
If I could make a guess I would say that somewhere in my project I need to reference where the database is on the server that I have deployed it to, but I really haven't got a clue where.
I have followed all the instructions in terms of deploying the database, and this worked fine but I guess it must be something to do with the database connection string?
If this is so what information can I provide to help you, help me find the problem?
Does your production DB have data in the tables already? is your connection string in the web.config correct? does your code reference the correct connection string?
Please remember to Mark As Answer if helpful
Regards, Christiandev (@chrisdev80), MCPD Web (2 & 4) & MCTS Windows (www.ScoreDonkey.com)
Am I right in excluding the standard connection string "default connection"? I also removed this when I imported my web.config on the package/Publish SQL tab. So I only have my
EDIT:
Almost forgot, locally I use SQL 2008 Express, our production database is 2008 R2.
If theres no data, that query will fail. Also, is that datasource correct in the 2nd web.config? It usually has server name/instance ? but this just be for where I host?
Can you connect to the hosting db via SSMS?
Please remember to Mark As Answer if helpful
Regards, Christiandev (@chrisdev80), MCPD Web (2 & 4) & MCTS Windows (www.ScoreDonkey.com)
public class OutageInitializer : DropCreateDatabaseIfModelChanges<OutageDBContext>
{
protected override void Seed(OutageDBContext context)
{
var outages = new List<Outage> {
new Outage { Title = "Scheduled Maintenance",
PostedOn=DateTime.Parse("2012-5-4"),
CompletedBy=DateTime.Parse("2012-5-4"),
Details="The system is currently offline whilst we perform some scheduled maintenance tasks. We expect the work to take no more than 2 hours. Please try back after 10:00 AM."}
};
outages.ForEach(d => context.Outages.Add(d));
}
}
I copied the connection string from the connection string in the image above (I then redid the image above to make sure the copied web config pulled that through.
The database and webservers are local, we don't use hosted we have everything in house.
I can access the database and the default line I have added is visible:
EDIT:
I have also deleted the autoscripts and used data and schema, this populated the line seen above in the database, but it is still the same when I go to the site.
Edit:
In terms of the connection string I tested through excel (I know probably not the best but it gives me a connection string) and this is what it gives me:
Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=user name;Data Source=server;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=computer;Use Encryption for Data=False;Tag with column collation when possible=False;Initial
Catalog=Outages
Does this look better and if so do I need to just exclude the workstation ID or is there any thing else that will need adding/ removing? Also does this need to go in the release web.config only or the main web.config and the publish sql tabs connection string
as well? Did I need to keep the default connection string as well ? When I tried to deploy that as well I got a table exists error for the meta data table.
mcinnes01
Member
16 Points
121 Posts
Help deploying code first website with database
May 03, 2012 01:19 PM|LINK
Hi,
I have been working through various tutorials and their links to instructions, to make and deploy code first web applications with databases.
The tutorial I followed for the application was the Movie tutorial. I used this tutorial for my own purposes (I have kept it simple at the moment) and I have the application working.
I came to trying to deploy (I use 1 click) and the first time I tried it failed as I hadn't configued the database side of things at all. So I followed a lot of instructions and I eventually got the site to deploy.
The first time I did this I checked the target server for the database (which is different to the application) and I couldn't see the database. The result was that when I went on to the application the site had caught the error on the sites general error.aspx that the MVC framework produces as standard.
I looked at little further on the SQL server and noticed it had added the database in the system databases, so I deleted them from here and created a new database called "Outages".
I then changed the various bits of code to reflect this and deployed again, this time the tables were created as expected, however the site still was not working from the live server.
I'm not sure where I am going wrong or how I can trace the error as it is just getting caught on the standard error page.
All the account pages work fine it is just the data related pages that produce the error.
Any help will be much appreciated.
Andy
raduenuca
All-Star
24675 Points
4250 Posts
Re: Help deploying code first website with database
May 03, 2012 01:50 PM|LINK
Use Elmah to log unhandled errros or simply set CustomErrors to off in web config. You should see the yellow screen with the error.
Radu Enuca | Blog
mcinnes01
Member
16 Points
121 Posts
Re: Help deploying code first website with database
May 03, 2012 02:08 PM|LINK
Hi raduenuca,
Do you mean this in my web.release.config?
<customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors>If so this is currently commented out, does that mean it is off?
mcinnes01
Member
16 Points
121 Posts
Re: Help deploying code first website with database
May 03, 2012 02:14 PM|LINK
Edit: Double Post
mcinnes01
Member
16 Points
121 Posts
Re: Help deploying code first website with database
May 03, 2012 02:14 PM|LINK
Hi,
I have a feeling it is something to do with this, I had the same problem once with an account model that I had customised and it was where I tried to reference something in the controller that was creating an error.
public ActionResult Details(int id = 0) { Outage outage = db.Outages.Find(id); if (outage == null) { return HttpNotFound(); } return View(outage); }So I have a feeling it is failing on the line: Outage outage = db.Outages.Find(id);
If I could make a guess I would say that somewhere in my project I need to reference where the database is on the server that I have deployed it to, but I really haven't got a clue where.
I have followed all the instructions in terms of deploying the database, and this worked fine but I guess it must be something to do with the database connection string?
If this is so what information can I provide to help you, help me find the problem?
Thanks in advance,
Andy
raduenuca
All-Star
24675 Points
4250 Posts
Re: Help deploying code first website with database
May 04, 2012 09:02 AM|LINK
Do you have a connection string in your web.config? And on your local machine you use SQL Express?
Radu Enuca | Blog
christiandev
Star
8607 Points
1841 Posts
Re: Help deploying code first website with database
May 04, 2012 09:10 AM|LINK
Does your production DB have data in the tables already? is your connection string in the web.config correct? does your code reference the correct connection string?
Regards, Christiandev (@chrisdev80), MCPD Web (2 & 4) & MCTS Windows (www.ScoreDonkey.com)
mcinnes01
Member
16 Points
121 Posts
Re: Help deploying code first website with database
May 04, 2012 09:19 AM|LINK
Hi,
I have put a drop table command in, so on my production database there is no data.
This is my connection string from the main web.config
<connectionStrings> <add name="OutageDBContext" connectionString="Data Source=|DataDirectory|Outages.sdf" providerName="System.Data.SqlServerCe.4.0"/> <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings>This is form my Web.Release.Config
<connectionStrings> <add name="OutageDBContext" connectionString="Data Source=OPADMTS;Initial Catalog=Outages;Persist Security Info=True;User ID=username;Password=password" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings>Am I right in excluding the standard connection string "default connection"? I also removed this when I imported my web.config on the package/Publish SQL tab. So I only have my

EDIT:
Almost forgot, locally I use SQL 2008 Express, our production database is 2008 R2.
christiandev
Star
8607 Points
1841 Posts
Re: Help deploying code first website with database
May 04, 2012 09:28 AM|LINK
If theres no data, that query will fail. Also, is that datasource correct in the 2nd web.config? It usually has server name/instance ? but this just be for where I host?
Can you connect to the hosting db via SSMS?
Regards, Christiandev (@chrisdev80), MCPD Web (2 & 4) & MCTS Windows (www.ScoreDonkey.com)
mcinnes01
Member
16 Points
121 Posts
Re: Help deploying code first website with database
May 04, 2012 09:38 AM|LINK
public class OutageInitializer : DropCreateDatabaseIfModelChanges<OutageDBContext> { protected override void Seed(OutageDBContext context) { var outages = new List<Outage> { new Outage { Title = "Scheduled Maintenance", PostedOn=DateTime.Parse("2012-5-4"), CompletedBy=DateTime.Parse("2012-5-4"), Details="The system is currently offline whilst we perform some scheduled maintenance tasks. We expect the work to take no more than 2 hours. Please try back after 10:00 AM."} }; outages.ForEach(d => context.Outages.Add(d)); } }I copied the connection string from the connection string in the image above (I then redid the image above to make sure the copied web config pulled that through.
The database and webservers are local, we don't use hosted we have everything in house.
I can access the database and the default line I have added is visible:
EDIT:
I have also deleted the autoscripts and used data and schema, this populated the line seen above in the database, but it is still the same when I go to the site.
Edit:
In terms of the connection string I tested through excel (I know probably not the best but it gives me a connection string) and this is what it gives me:
Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=user name;Data Source=server;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=computer;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=Outages
Does this look better and if so do I need to just exclude the workstation ID or is there any thing else that will need adding/ removing? Also does this need to go in the release web.config only or the main web.config and the publish sql tabs connection string as well? Did I need to keep the default connection string as well ? When I tried to deploy that as well I got a table exists error for the meta data table.