Showing posts with label MVC EF. Show all posts
Showing posts with label MVC EF. Show all posts

Thursday, March 26, 2015

Someone's MVC EF Project Won't Build - Things You Always Forget

Tools >Library Package Manager > Package Manager Settings.

Then check Allow NuGet to download missing packages during build.

Then rebuild the solution, and it should work.

BUT THIS WON'T WORK IF YOU HAVE AN OLD VERSION OF NUGET PACKAGE MANAGER!
Version 2.4 won't work, perhaps not even 2.6. Version 2.8 did seem to work.

Tuesday, November 25, 2014

Sure, throw a null-coalescing operator into an intro tutorial on MVC EF


"
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(students.ToPagedList(pageNumber, pageSize));
The ToPagedList method takes a page number. The two question marks represent the null-coalescing operator. The null-coalescing operator defines a default value for a nullable type; the expression (page ?? 1) means return the value of page if it has a value, or return 1 if page is null.
"

Can't be surprised since he loves the Ternary Operator.



Some links on Ternary Operators:

http://stackoverflow.com/questions/694814/ternary-operator-bad-or-best-practice

http://programmers.stackexchange.com/questions/28314/ternary-operator-considered-harmful

http://thedailywtf.com/articles/One-Bad-Ternary-Operator-Deserves-Another

https://agiletribe.wordpress.com/2011/11/01/21-avoid-ternary-conditional-operator/

http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixpost=142517

http://en.wikipedia.org/wiki/Talk%3A%3F%3A
"I don't know how to do so, but this page should be flagged as being a very biased viewpoint. The text runs counter to my experience of 20+ years as a professional software programmer. The only people who prefer the ternary conditional operator to the more readable if-then-else format (when both are allowed by the language syntax) are those people who have never had to maintain other people's code bases for any significant period of time. This opinion is found in industry standard texts, such as "Code Complete" by Steve McConnell. I would love to see a valid citation supporting the viewpoint presented here, i.e. that the ternary operator is NOT considered to be antiquated, harder to debug, and harder to maintain."

Thursday, November 20, 2014

Random MVC EF stuff

"The Required attribute is not needed for value types such as DateTime, int, double, and float. Value types cannot be assigned a null value, so they are inherently required. "

"

The ForeignKey Attribute

When there is a  one-to-zero-or-one relationship or a  one-to-one relationship between two entities (such as betweenOfficeAssignment and Instructor), EF can't work out which end of the relationship is the principal and which end is dependent.  One-to-one relationships have a reference navigation property in each class to the other class. TheForeignKey Attribute can be applied to the dependent class to establish the relationship. If you omit the ForeignKey Attribute, you get the following error when you try to create the migration:
Unable to determine the principal end of an association between the types 'ContosoUniversity.Models.OfficeAssignment' and 'ContosoUniversity.Models.Instructor'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."



..later one we learn:

"The course entity has a foreign key property DepartmentID which points to the related Department entity and it has aDepartment navigation property. The Entity Framework doesn't require you to add a foreign key property to your data model when you have a navigation property for a related entity.  EF automatically creates foreign keys in the database wherever they are needed. But having the foreign key in the data model can make updates simpler and more efficient. For example, when you fetch a course entity to edit, the  Department entity is null if you don't load it, so when you update the course entity, you would have to first fetch the  Department entity. When the foreign key propertyDepartmentID is included in the data model, you don't need to fetch the Department entity before you update."