0

In an MVC application I'm using Entity Framework for this simple model:

public class Product
{
    public Guid ProductID { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 1)]
    [Display(Name = "Product name")]
    public string ProductName { get; set; }

    [Required]
    [Display(Name = "Download URL")]
    [DataType(DataType.Url)]
    public string DownloadUrl { get; set; }

    [Display(Name = "Download counter")]
    public int DownloadCounter { get; set; }
}

I've have scaffold pages for this automatically and have two questions:

PEK
  • 3,049
  • 2
  • 26
  • 43
  • 1
    To hide a property in a generated view use this DataAttribute: `[ScaffoldColumn(false)]`. As for your second question, i guess we need to see your Controller method for the edit POST action. – Marco Jul 17 '14 at 20:54
  • Ah, thanks, that's brilliant :-) – PEK Jul 17 '14 at 21:03

1 Answers1

0

Ups, I should have spend some more time on searching. Answer to the second question is here:

Exclude Property on Update in Entity Framework

Community
  • 1
  • 1
PEK
  • 3,049
  • 2
  • 26
  • 43
  • that approach should be unnecessary. unless you have disabled tracking, when a object is loaded into the Context, eg Find(123) the instance 123 is tracked so that obj.prop2 = "xyz" would be noted by EF. So when you do a SaveChanges, only the changed value is sent back – phil soady Jul 18 '14 at 05:14
  • Oh, that's interesting. As I said if I change DownloadCounter directly in the database when I'm on the products edit page and then select to save the product the DownloadCounter gets the value it had before when the edit page was loaded (this seems to be true for other columns as well). Tracking hasn't been changed. My idea was to increase this value with a raw sql-query so counts is never lost. But could I instead load the entity with Entity framework, increase it and save it to database with the same safe result? What if I had several web instances with one database - would it be safe then? – PEK Jul 18 '14 at 06:59
  • run sql profiler and check the SQL statement sent, if you are using AddOrUpdate then the whole is always sent. Other if you "fetch" using find , Load, firstOrDefault... then the object is loaded into the context. If you use a Project... select(partial fields) then you do not get tracking. – phil soady Jul 19 '14 at 02:19