I keep forgetting the syntax for shorthand of if-then-else.
condition ? expression_for_true : expression_for_false
Example
return (MyBlogPosts.Count() != 0) ? System.IO.File.Create("totalPosts.txt") : false;
I keep forgetting the syntax for shorthand of if-then-else.
condition ? expression_for_true : expression_for_false
Example
return (MyBlogPosts.Count() != 0) ? System.IO.File.Create("totalPosts.txt") : false;
Ninject is very cool and lightweight DI container.
But you can not always inject objects through constructor – sometimes you must have parameter-less constructor, like in MvcApplication object.
In that case you can use DependencyResolver from System.Web.Mvc namespace from System.Web assembly
_userDataService = DependencyResolver.Current.GetService<IUserDataService>();
Unfortunately MSDN documentation does not have much, only link to this blog post: http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html
In Razor compiler directives do not work, cause Razor Views are not compiled.
So this would not work:
@{ #if DEBUG} @{#else} @{#endif}
But you can trick that thing into submission, thanks to this StackOverflow topic
A better, more generic solution is to use an extension method, so all views have access to it:
public static bool IsReleaseBuild() { #if DEBUG return false; #else return true; #endif }
You can then use it like follows in any view (razor syntax):
@if(IsReleaseBuild())
If Entity Framework (version below 5.1) on your attempt to Update-Database gives you error of “Object reference not set”, change the default start-up project to MVC project, not anything else. And try the same thing again.
This reported to be fixed in the latest 5.x version of EF, but it is a little annoyance just now.
p.s. This can be eased with parameter: -StartupProject “helloMVC” when running Update-Database command
Visual Studio Code snippets are handy way of increasing your speed of development.
User keyboard shortcuts
CTRL+K + X for code snippet
CTRL+K + S for Surround With snippet
Here are most of the described: http://msdn.microsoft.com/en-us/library/z41h7fat%28v=vs.100%29.aspx
Or you can download this Word document and print them out – the same information, only in printer-friendly view.
Getting error “Asp.net 4.0 has not been registered on the web server.. blah-blah”?
Run this command as Administrator:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -i
This will register ASP with IIS.
Original: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
Enable-Migrations
Add-Migration – scaffold next migration based on changes made to the model.
Update-Database – update database model to the latest migration model
Update-Database -Verbose – update database model and output SQL generated
Update-Database -TargetMigration: “someMigration” – update database up (or down) to a “revision”
Update-Database -Script -SourceMigration:$InitialDatabase -TargetMigration:”AddPostAbstract” Generate SQL script of the migrations
SQL(“update sometable set somefield=’hi'”) – can insert custom SQL into migration class.
Original here: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx
Enable-Migrations -EnableAutomaticMigrations -Force – turn on automatic migrations
Basically does the same as code-based migrations, but before doing Database-update it automatically calls Add-Migration and gives it a name.
set the form’s KeyPreview property to true, and add this override to the Form code:
protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.KeyCode == Keys.Escape) this.Close(); }
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13189 Or here: http://www.itscodingtime.com/post/Visual-Studio-2010-Keyboard-Mouse-Shortcuts.aspx