ASP NET Compression Caching

modify web.config
  
    
      
    
    
      
    
    
      
      
        
        
        
        
        
        
        
      
      
        
        
        
        
        
        
        
      
    
    
  
add attribute where it requers
        [OutputCache(Duration = int.MaxValue, VaryByParam = "none")]
        public ActionResult Index()
        {
            return View();
        }

ASP .NET MVC 5 Validation and Way to change standard validation message

my view
@model Riski.Models.Koef

@using (Html.BeginForm()) {
    

    @Html.AntiForgeryToken()
    

@Model.name


@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.koef1, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.koef1, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.koef1, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.id) @Html.HiddenFor(model => model.name)
}
@Html.ActionLink("Вернуться к списку", "Index")
@section scripts{ @Scripts.Render("~/bundles/jqueryval") }
my controller
        [HttpPost]
        public ActionResult Edit(Koef koef)
        {
            if (ModelState.IsValid)
            {
                db.Configuration.ValidateOnSaveEnabled = false;

                db.Koefs.Attach(koef);
                var entry = db.Entry(koef);
                entry.Property(e => e.koef1).IsModified = true;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(koef);
        }
this is a partial class wich helps us to validate our model. Acording to this approach we don't need to add Validation Attributies after every process of generation Entity classes
using System;
using System.ComponentModel.DataAnnotations;

namespace Riski.Models
{
    [MetadataType(typeof(KoefViewModel))]
    public partial class Koef
    {
    }

    public class KoefViewModel
    {
        [Required]
        [Display(Name = "Коэффициент")]
        [Range(0, 100, ErrorMessage = "Значение должно быть от 0 до 100.")]
        public Nullable koef1 { get; set; }

        [Required]
        public int id { get; set; }
    }
}
That's it. Now , as this is a russian web-site, we need to override some standard validation messages. It's simple solution and we should do only for steps

1 Add folder

2 Create resource file

3 Add new value (We can override 4 types of messages FieldMustBeDateFieldMustBeNumericPropertyValueInvalid andPropertyValueRequired)


4 And add one line to your Global.asax.cs
DefaultModelBinder.ResourceClassKey = "MyNewResource";

Creating stub methods using ShortCut in VS2013

http://dailydotnettips.com/2011/01/01/generate-method-stubs-using-shortcut-key-in-visual-studio/

just press "ctrl + ." and "Enter"

c# run thread in threadpool

This is the way of running thread in ThreadPool
1 only for 4.5 Framework
Task.Run(() => doStuff("hello world"));
2
Task.Factory.StartNew(() => doStuff("hello world"));
3
ThreadPool.QueueUserWorkItem(a => doStuff("hello world"));
4
void Example()
{
    // Hook up the ProcessFile method to the ThreadPool.
    // Note: 'a' is an argument name. Read more on arguments.
    ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessFile), a);
}

private void ProcessFile(object a)
{
    // I was hooked up to the ThreadPool by WaitCallback.
}

sql management studio allow drop and recreate


EntityFramework exec proc with out params


using (var db = new MyEntities())
{
    var Owner_ID = new ObjectParameter("Owner_ID", typeof(string));
    var msgError = new ObjectParameter("msgError", typeof(string));
    db.InsBeerClient(Owner_ID, msgError);
    return (msgError.Value ?? "").ToString();
}

Entity Framework Update

You can easly do it by using this code


using (var db = new EGAIS_RUEntities())
            {
                db.Configuration.ValidateOnSaveEnabled = false;
                var _rec = new CONTRAGENT()
                {
                    id = 1,
                    status = 3
                };

                db.CONTRAGENTS.Attach(_rec);
                var entry = db.Entry(_rec);
                entry.Property(e => e.status).IsModified = true;
                db.SaveChanges();
            }

Please note, I set only value, that i wanted to cahnge. Another value is "id", that identifies the record.

Another interesting aspect is this

db.Configuration.ValidateOnSaveEnabled = false;

You will get an error if you don't set the fields with NOTNULL values.

another approach to it is
dbcontext.Entry(_rec).State = System.Data.Entity.EntityState.Modified;
dbcontext.SaveChanges();