Wednesday 27 October 2010

How to Make LINQ to SQL Check for Changes After Attach (or AttachAll)

dbContext.Entity.Attach(entity, true);
//submit insert or update to db
dbContext.SubmitChanges();

Problem: entity was saved, but its dependencies weren't.

Solution 1: Try to loop its dependencies and attach manually
    foreach (var child in entity.children)
   {
          dbContext.Child.Attach(child);
   
          get error at this line: An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.
   }

- Solution 1 is completely wrong to resolve the problem
Root cause of Problem: LINQ to SQL assumes that the object you are attaching is un-modified. True, there is an overloaded method "AttachAll(items, true)" that forces LINQ to view those objects as 'modified', but that method only works if your table has a TimeStamp field.

How to resolve: So, how do I tell LINQ to SQL to see if there have been changes made to already-existing entities? By using the "Refresh" method:
// Add this line before "SubmitChanges"
dataContext.Refresh(RefreshMode.KeepCurrentValues, entity);

Completed code as following
.Entity.Attach(entity, true);
dbContext.Child.InsertAllOnSubmit(entity.Children.Where(r => r.Id == 0));

dbContext.Refresh(RefreshMode.KeepCurrentValues, entity);
dbContext.Refresh(RefreshMode.KeepCurrentValues, entity.Children.Where(r => r.Id != 0));


//submit insert or update to db
dbContext.SubmitChanges();

Sunday 10 October 2010

ASP.NET MVC

============ LESSON LEARN ========
Model: consist of business logic, data access logic
View: Markup and content that is returned to the browser
Controller: Fires off some logic and response to a browser request

MAPPING URL ROUTING
Global.asax.cs: Using <routes> table to map url and its parameters

MUST DO
1. Follow MVC framework naming convention

============ RESOURCE ===========
1. Download ASP.NET MVC 2 framework
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c9ba1fe1-3ba8-439a-9e21-def90a8615a9&displaylang=en

2. Understanding overview of ASP.NET MVC
http://www.asp.net/mvc/videos/understanding-models-views-and-controllers

3.Five Must Have Tools for MVC Developers
http://www.codecapers.com/post/Five-Must-Have-Tools-for-MVC-Developers.aspx

Visual Studio 2008 (Some shorcut keys)

Here are some extremely helpful keyboard shortcut keys for Visual Studio 2008. You probably know most, but some you won't know. For instance, did you know that Ctrl+. is the same as Sift+Alt+F10?
Note: Some of the shortcuts won't work in all environment configurations (web, C#, etc).
To use, hold down the control key and hit the key combination. Works on the whole document, a specific line, or a selection of code.
Go to Definition (F12) Navigate to the definition of the method or variable. Super useful.
Find all References (Shift + F12)
Update References/ Add Using Statement (Ctrl +. or Shift +Alt +F10) I'm referring to the underline that appears under a variable to perform an action like add a using statement, or refacter a method or class name. Not sure what it's called but I use it all the time. Ex: ClassName
List Members (Ctrl +J or Ctrl +K, L) Displays the autocomplete list for classes, methods, or properties.
List Parameter Info (Ctrl +Shift +Space or Ctrl +K +P) Lists the parameters for a method. Use this shortcut when your cursor is inside the parenthesis of a method.
Auto Format (Ctrl +K, D or Ctrl +E, D) Formats C#, VB, ASPX, Javascript, CSS, XML, XSL, and HTML code according to generally accepted formatting standards for the respective programming language. Makes code look nice and neat.
Quick Watch (Ctrl +Alt +Q) Highlight some code and hit these shortcut keys to display the Quick Watch dialog.
Comment / Uncomment (Ctrl +K, C / Ctrl +K, U) Comments or uncomments a block of code. Works with C#, VB, ASPX.
Code Bookmarks Use these to bookmark a line of code. You can cycle through the bookmarks across any file within a project. Bookmarks come in very handy, try it out.
Set/Unset Bookmark (Ctrl +K, K) Next Bookmark (Ctrl +K, N)
Surround With (Ctrl +S) Surrounds a block of code with a code snippet.
Build & Debug Shortcuts Set/Unset Breakpoint (F9) Build (F6 or Ctrl +Shift +B) Start Debugging (F5) Start Without Debugging (Ctrl +F5) Stop Debugging (Shift +F5) Continue (F5) Step Over (F10) Step Into (F11) Step Out (Shift +F11) Attach to Process (Ctrl + Alt + P)
That's it for now, feel free to add more in the comments below. I'll try to update this post periodically.

Copy from Rob Volk's Dev Blog
http://blog.robvolk.com/2009/01/visual-studio-2008-keyboard-shortcuts.html