{"id":219947,"date":"2014-04-18T19:21:02","date_gmt":"2014-04-18T15:21:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=219947"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=219947","title":{"rendered":"<span class=\"post_title\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0440\u043e\u0432\u043d\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u043d\u0430 Entity Framework Code First<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/3b3\/256\/4a8\/3b32564a8b65eeecbf6cc9240a1dd00b.png\"\/><\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e!<\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0442\u043e\u043f\u0438\u043a\u0435 \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0441\u043b\u043e\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c (Data Access Level) \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a Entity Framework-\u0443, \u0434\u0430\u043b\u0435\u0435 EF, \u043e \u0442\u043e\u043c \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0442\u043e\u044f\u043b\u0438 \u0438 \u043a\u0430\u043a \u044f \u0438\u0445 \u0440\u0435\u0448\u0438\u043b. \u0412\u0435\u0441\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u0434 \u0438\u0437 \u043f\u043e\u0441\u0442\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 <a href=\"http:\/\/yadi.sk\/d\/P9XDDznpMj6p8\">\u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u043d\u044b\u0439 \u0434\u0435\u043c\u043e \u043f\u0440\u043e\u0435\u043a\u0442 <\/a> \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0434 \u043b\u0438\u0431\u0435\u0440\u0430\u043b\u044c\u043d\u043e\u0439 \u043b\u0438\u0446\u0435\u043d\u0437\u0438\u0435\u0439 MIT, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043a\u0430\u043a \u0432\u0430\u043c \u0443\u0433\u043e\u0434\u043d\u043e.<br \/>  \u0421\u0440\u0430\u0437\u0443 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u043d\u0443\u0442\u044c, \u0447\u0442\u043e \u0432\u0435\u0441\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 2-\u0445 \u043b\u0435\u0442 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043a\u0440\u0443\u043f\u043d\u043e\u0439 \u0440\u043e\u0441\u0441\u0438\u0439\u0441\u043a\u0438\u0439 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u043d\u043e \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0432\u044b\u0441\u043e\u043a\u043e\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c.<\/p>\n<p>  \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h5>\u0417\u0430\u0434\u0430\u0447\u0438<\/h5>\n<p>  \u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u0441\u0442\u043e\u044f\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0434\u0430\u0447 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0441\u043b\u043e\u044e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c:<br \/>  1. \u0412\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u043d\u044b \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c \u043a\u0430\u043a\u043e\u0439 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b<br \/>  2. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 <a href=\"http:\/\/habrahabr.ru\/post\/52173\/\">\u00ab\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439\u00bb <\/a> <br \/>  3. \u041a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043d\u0430\u0434 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0442\u043e \u0435\u0441\u0442\u044c \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442, \u0442\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442.<br \/>  \u041f\u043e\u044f\u0441\u043d\u044e: <br \/>  \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, EF \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 NHibernate. \u0422\u0430\u043a\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0447\u0440\u0435\u0432\u0430\u0442\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u0440\u043e\u0434\u0430 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u044b\u043c\u0438 \u043e\u0448\u0438\u0431\u043a\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0434\u0432\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043d\u043e \u0445\u043e\u0447\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u044d\u0442\u0438 \u0434\u0432\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b \u0441 \u043e\u0434\u0438\u043d \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, EF \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u043e\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. <\/p>\n<h5>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h5>\n<p>  \u041a\u043e\u0434\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043d\u043e\u0433\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e \u043a \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u0430\u043c.<br \/>  \u041d\u0430\u0447\u043d\u0443 \u043f\u043e\u0436\u0430\u043b\u0443\u0439 \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  \u0412 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c \u0438 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435, \u043e\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">UsersContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace TestApp.Models {     public partial class UsersContext : DbContext     {          public UsersContext()             : base(&quot;Name=UsersContext&quot;)         {         }          public DbSet&lt;User&gt; Users { get; set; }          protected override void OnModelCreating(DbModelBuilder modelBuilder)         {             modelBuilder.Configurations.Add(new UserMap());         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0435\u0433\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">IDbContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">    public interface IDbContext     {         IQueryable&lt;T&gt; Find&lt;T&gt;() where T : class;          void MarkAsAdded&lt;T&gt;(T entity) where T : class;          void MarkAsDeleted&lt;T&gt;(T entity) where T : class;          void MarkAsModified&lt;T&gt;(T entity) where T : class;          void Commit(bool withLogging);         \/\/\u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u0441\u0435\u0445 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u0445         void Rollback();          \/\/ \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0438\u043b\u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432         void EnableTracking(bool isEnable);          EntityState GetEntityState&lt;T&gt;(T entity) where T : class;          void SetEntityState&lt;T&gt;(T entity, EntityState state) where T : class;          \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441 \u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c         DbChangeTracker GetChangeTracker();          DbEntityEntry GetDbEntry&lt;T&gt;(T entity) where T : class;     } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0439\u0441\u044f \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 DbContext:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">DemoAppDbContext<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace DataAccess.DbContexts {     public class DemoAppDbContext : DbContext, IDbContext     {         public static User CurrentUser { get; set; }          private readonly ILogger _logger;          #region Context Entities          public DbSet&lt;EntityChange&gt; EntityChanges { get; set; }          public DbSet&lt;User&gt; Users { get; set; }          #endregion          static DemoAppDbContext()         {             \/\/\u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440             Database.SetInitializer(new CreateDBContextInitializer());         }          \/\/ \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445         public static void Seed(DemoAppDbContext context)         {             \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e             var defaultUser = new User { Email = &quot;UserEmail@email.ru&quot;, Login = &quot;login&quot;, IsBlocked = false, Name = &quot;Vasy Pupkin&quot; };             context.Users.Add(defaultUser);             context.SaveChanges();         }          public DemoAppDbContext(string nameOrConnectionString)             : base(nameOrConnectionString)         {            \/\/ \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043b\u043e\u0433\u0433\u0435\u0440\u0430             _logger = new Logger(this);         }          protected override void OnModelCreating(DbModelBuilder modelBuilder)         {              modelBuilder.Configurations.Add(new EntityChangeMap());             modelBuilder.Configurations.Add(new UserMap());         }          public void MarkAsAdded&lt;T&gt;(T entity) where T : class         {             Entry(entity).State = EntityState.Added;             Set&lt;T&gt;().Add(entity);         }          public void MarkAsDeleted&lt;T&gt;(T entity) where T : class         {             Attach(entity);             Entry(entity).State = EntityState.Deleted;             Set&lt;T&gt;().Remove(entity);         }          public void MarkAsModified&lt;T&gt;(T entity) where T : class         {             Attach(entity);             Entry(entity).State = EntityState.Modified;         }          public void Attach&lt;T&gt;(T entity) where T : class         {             if (Entry(entity).State == EntityState.Detached)             {                 Set&lt;T&gt;().Attach(entity);             }         }          public void Commit(bool withLogging)         {             BeforeCommit();             if (withLogging)             {                 _logger.Run();             }             SaveChanges();         }          private void BeforeCommit()         {             UndoExistAddedEntitys();         }          \/\/\u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0443 \u0435\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u043a\u0430\u043a  \u043d\u043e\u0432\u044b\u0435, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445         private void UndoExistAddedEntitys()         {             IEnumerable&lt;DbEntityEntry&gt; dbEntityEntries = GetChangeTracker().Entries().Where(x =&gt; x.State == EntityState.Added);             foreach (var dbEntityEntry in dbEntityEntries)             {                 if (GetKeyValue(dbEntityEntry.Entity) &gt; 0)                 {                     SetEntityState(dbEntityEntry.Entity, EntityState.Unchanged);                 }             }         }          \/\/ \u043e\u0442\u043a\u0430\u0442 \u0432\u0441\u0435\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u0445         public void Rollback()         {             ChangeTracker.Entries().ToList().ForEach(x =&gt; x.Reload());         }          public void EnableTracking(bool isEnable)         {             Configuration.AutoDetectChangesEnabled = isEnable;         }          public void SetEntityState&lt;T&gt;(T entity, EntityState state) where T : class         {             Entry(entity).State = state;         }          public DbChangeTracker GetChangeTracker()         {             return ChangeTracker;         }          public EntityState GetEntityState&lt;T&gt;(T entity) where T : class         {             return Entry(entity).State;         }          public IQueryable&lt;T&gt; Find&lt;T&gt;() where T : class         {             return Set&lt;T&gt;();         }          public DbEntityEntry GetDbEntry&lt;T&gt;(T entity) where T : class         {             return Entry(entity);         }          public static int GetKeyValue&lt;T&gt;(T entity) where T : class         {             var dbEntity = entity as IDbEntity;             if (dbEntity == null)                 throw new ArgumentException(&quot;Entity should be IDbEntity type - &quot; + entity.GetType().Name);              return dbEntity.GetPrimaryKey();         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0412\u0441\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 CRUD \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">IRepository<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">    interface IRepository&lt;T&gt; where T : class     {         DemoAppDbContext CreateDatabaseContext();          List&lt;T&gt; GetAll();          T Find(int entityId);          T SaveOrUpdate(T entity);          T Add(T entity);          T Update(T entity);          void Delete(T entity);          \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0448\u0438\u0431\u043e\u043a         DbEntityValidationResult Validate(T entity);         \/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043e\u0448\u0438\u0431\u043a\u0430\u043c\u0438        string ValidateAndReturnErrorString(T entity, out bool isValid);     } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f IRepository:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">BaseRepository<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace DataAccess.Repositories {     public abstract class BaseRepository&lt;T&gt; : IRepository&lt;T&gt; where T : class     {         private readonly IContextManager _contextManager;          protected BaseRepository(IContextManager contextManager)         {             _contextManager = contextManager;         }          public DbEntityValidationResult Validate(T entity)         {             using (var context = CreateDatabaseContext())             {                 return context.Entry(entity).GetValidationResult();             }         }          public string ValidateAndReturnErrorString(T entity, out bool isValid)         {             using (var context = CreateDatabaseContext())             {                 DbEntityValidationResult dbEntityValidationResult = context.Entry(entity).GetValidationResult();                 isValid = dbEntityValidationResult.IsValid;                 if (!dbEntityValidationResult.IsValid)                 {                     return DbValidationMessageParser.GetErrorMessage(dbEntityValidationResult);                 }                 return string.Empty;             }         }          \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c using         public DemoAppDbContext CreateDatabaseContext()         {             return _contextManager.CreateDatabaseContext();         }                public List&lt;T&gt; GetAll()         {             using (var context = CreateDatabaseContext())             {                 return context.Set&lt;T&gt;().ToList();             }         }          public T Find(int entityId)         {             using (var context = CreateDatabaseContext())             {                 return context.Set&lt;T&gt;().Find(entityId);             }         }          \/\/ \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434. \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0434 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u0430\u0445         protected virtual void BeforeSave(T entity, DemoAppDbContext db)         {                      }          public T SaveOrUpdate(T entity)         {             var iDbEntity = entity as IDbEntity;              if (iDbEntity == null)                 throw new ArgumentException(&quot;entity should be IDbEntity type&quot;, &quot;entity&quot;);              return iDbEntity.GetPrimaryKey() == 0 ? Add(entity) : Update(entity);         }                public T Add(T entity)         {             using (var context = CreateDatabaseContext())             {                 BeforeSave(entity, context);                 context.MarkAsAdded(entity);                 context.Commit(true);             }             return entity;         }          public T Update(T entity)         {             using (var context = CreateDatabaseContext())             {                 var iDbEntity = entity as IDbEntity;                 if (iDbEntity == null)                     throw new ArgumentException(&quot;entity should be IDbEntity type&quot;, &quot;entity&quot;);                  var attachedEntity = context.Set&lt;T&gt;().Find(iDbEntity.GetPrimaryKey());                 context.Entry(attachedEntity).CurrentValues.SetValues(entity);                                BeforeSave(attachedEntity, context);                 context.Commit(true);             }             return entity;         }          public void Delete(T entity)         {             using (var context = CreateDatabaseContext())             {                 context.MarkAsDeleted(entity);                 context.Commit(true);             }         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041e\u0431\u044a\u0435\u043a\u0442 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 User:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">User<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace DataAccess.Models {     public class User : IDbEntity     {         public User()         {             this.EntityChanges = new List&lt;EntityChange&gt;();         }          public int UserId { get; set; }          [Required(AllowEmptyStrings = false, ErrorMessage = @&quot;Please input Login&quot;)]         [StringLength(50, ErrorMessage = @&quot;Login \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435 50 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432&quot;)]         public string Login { get; set; }          [Required(AllowEmptyStrings = false, ErrorMessage = @&quot;Please input Email&quot;)]         [StringLength(50, ErrorMessage = @&quot;Email \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435 50 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432&quot;)]         public string Email { get; set; }          [Required(AllowEmptyStrings = false, ErrorMessage = @&quot;Please input Name&quot;)]         [StringLength(50, ErrorMessage = @&quot;\u0418\u043c\u044f \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435 50 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432&quot;)]         public string Name { get; set; }          public bool IsBlocked { get; set; }          public virtual ICollection&lt;EntityChange&gt; EntityChanges { get; set; }          public override string ToString()         {             return string.Format(&quot;\u0422\u0438\u043f: User; \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435:{0}, UserId:{1} &quot;, Name, UserId);         }          public int GetPrimaryKey()         {             return UserId;         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u00abUser\u00bb, c \u0440\u044f\u0434\u043e\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u044e\u0449\u0438\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 CRUD \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">UsersRepository<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace DataAccess.Repositories {     public class UsersRepository : BaseRepository&lt;User&gt;     {         public UsersRepository(IContextManager contextManager)             : base(contextManager)         {          }          public User FindByLogin(string login)         {             using (var db = CreateDatabaseContext())             {                 return db.Set&lt;User&gt;().FirstOrDefault(u =&gt; u.Login == login);             }         }          public bool ExistUser(string login)         {             using (var db = CreateDatabaseContext())             {                 return db.Set&lt;User&gt;().Count(u =&gt; u.Login == login) &gt; 0;             }         }          public User GetByUserId(int userId)         {             using (var db = CreateDatabaseContext())             {                 return db.Set&lt;User&gt;().SingleOrDefault(c =&gt; c.UserId == userId);             }          }          public User GetFirst()         {             using (var db = CreateDatabaseContext())             {                 return db.Set&lt;User&gt;().First();             }         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0432\u0441\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u0441\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0439 service locator RepositoryContainer. \u042d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043b\u043e \u0434\u043b\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u043e\u0432.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">RepositoryContainer<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace DataAccess.Container {     public class RepositoryContainer     {         private readonly IContainer _repositoryContainer = new Container();          public static readonly RepositoryContainer Instance = new RepositoryContainer();          private RepositoryContainer()         {                      }          public T Resolve&lt;T&gt;() where T : class         {             return _repositoryContainer.Resolve&lt;T&gt;();         }          public void Register&lt;T&gt;(T entity) where T : class         {             _repositoryContainer.Register(entity);         }     } }  namespace DataAccess.Container {     public static class RepositoryContainerFactory     {         public static void RegisterAllRepositories(IContextManager dbContext)         {             RepositoryContainer.Instance.Register(dbContext);             RepositoryContainer.Instance.Register(new EntityChangesRepository(dbContext));             RepositoryContainer.Instance.Register(new UsersRepository(dbContext));         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412\u0441\u0435\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f\u043c, \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 IContextManager, \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0434\u043b\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u043c\u0438 \u0438 \u0438\u0445 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">IContextManager<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace DataAccess.Interfaces {     public interface IContextManager     {         DemoAppDbContext CreateDatabaseContext();     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f ContextManager:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ContextManager<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">using DataAccess.Interfaces;  namespace DataAccess.DbContexts {     public class ContextManager : IContextManager     {         private readonly string _connectionString;          public ContextManager(string connectionString)         {             _connectionString = connectionString;         }          public DemoAppDbContext CreateDatabaseContext()         {             return new DemoAppDbContext(_connectionString);         }     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 ILogger:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ILogger<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">namespace DataAccess.Interfaces {     internal interface ILogger     {         void Run();     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 ILogger   <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Logger<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\"> public class Logger : ILogger     {         Dictionary&lt;EntityState, string&gt; _operationTypes;          private readonly IDbContext _dbContext;          public Logger(IDbContext dbContext)         {             _dbContext = dbContext;             InitOperationTypes();         }          public void Run()         {             LogChangedEntities(EntityState.Added);             LogChangedEntities(EntityState.Modified);             LogChangedEntities(EntityState.Deleted);         }          private void InitOperationTypes()         {             _operationTypes = new Dictionary&lt;EntityState, string&gt;                 {                     {EntityState.Added, &quot;\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435&quot;},                     {EntityState.Deleted, &quot;\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435&quot;},                     {EntityState.Modified, &quot;\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435&quot;}                 };         }          private string GetOperationName(EntityState entityState)         {             return _operationTypes[entityState];         }          private void LogChangedEntities(EntityState entityState)         {             IEnumerable&lt;DbEntityEntry&gt; dbEntityEntries = _dbContext.GetChangeTracker().Entries().Where(x =&gt; x.State == entityState);             foreach (var dbEntityEntry in dbEntityEntries)             {                 LogChangedEntitie(dbEntityEntry, entityState);             }         }          private void LogChangedEntitie(DbEntityEntry dbEntityEntry, EntityState entityState)         {             string operationHash = HashGenerator.GenerateHash(10);             int enitityId = DemoAppDbContext.GetKeyValue(dbEntityEntry.Entity);              Type type = dbEntityEntry.Entity.GetType();              IEnumerable&lt;string&gt; propertyNames = entityState == EntityState.Deleted                                                     ? dbEntityEntry.OriginalValues.PropertyNames                                                     : dbEntityEntry.CurrentValues.PropertyNames;              foreach (var propertyName in propertyNames)             {                 DbPropertyEntry property = dbEntityEntry.Property(propertyName);                  if (entityState == EntityState.Modified && !property.IsModified)                     continue;                  _dbContext.MarkAsAdded(new EntityChange                 {                     UserId = DemoAppDbContext.CurrentUser.UserId,                     Created = DateTime.Now,                     OperationHash = operationHash,                     EntityName = string.Empty,                     EntityType = type.ToString(),                     EntityId = enitityId.ToString(),                     PropertyName = propertyName,                     OriginalValue =                         entityState != EntityState.Added && property.OriginalValue != null                             ? property.OriginalValue.ToString()                             : string.Empty,                     ModifyValue =                         entityState != EntityState.Deleted && property.CurrentValue != null                         ? property.CurrentValue.ToString()                         : string.Empty,                     OperationType = GetOperationName(entityState),                 });             }         }     } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<h5>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <\/h5>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432:   <\/p>\n<pre><code class=\"cs\">RepositoryContainerFactory.RegisterAllRepositories(new ContextManager(Settings.Default.DBConnectionString)); <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u043e\u0439\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u042d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0432 \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0434\u0435\u043b\u0430\u043b \u0442\u043e \u0438\u043b\u0438 \u0438\u043d\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435. \u0412 \u0434\u0435\u043c\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u044d\u0442\u043e\u0442 \u043f\u0443\u043d\u043a\u0442 \u0443\u043f\u0443\u0449\u0435\u043d.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">InitDefaultUser<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">private void InitDefaultUser() { \tUser defaultUser = RepositoryContainer.Instance.Resolve&lt;UsersRepository&gt;().GetFirst(); \tDemoAppDbContext.CurrentUser = defaultUser; } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412\u044b\u0437\u043e\u0432 \u043a \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0443 service locator-a. \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0438\u0434\u0435\u0442 \u043a \u043c\u0435\u0442\u043e\u0434\u0443 GetFirst() \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0442\u0438\u043f\u0430 UsersRepository:  <\/p>\n<pre><code class=\"cs\">User defaultUser = RepositoryContainer.Instance.Resolve&lt;UsersRepository&gt;().GetFirst(); <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:  <\/p>\n<pre><code class=\"cs\">var newUser = new User { Email = &quot;UserEmail@email.ru&quot;, Login = &quot;login&quot;, IsBlocked = false, Name = &quot;Vasy Pupkin&quot;}; RepositoryContainer.Instance.Resolve&lt;UsersRepository&gt;().SaveOrUpdate(newUser); <\/code><\/pre>\n<p>  <\/p>\n<h5>\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0434 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/h5>\n<p>  \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a:  <\/p>\n<pre><code class=\"cs\">var newUser = new User { Email = &quot;UserEmail@email.ru&quot;,  IsBlocked = false,  }; DbEntityValidationResult dbEntityValidationResult = RepositoryContainer.Instance.Resolve&lt;UsersRepository&gt;().Validate(newUser); <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u043e\u0448\u0438\u0431\u043a\u0430\u043c\u0438:  <\/p>\n<pre><code class=\"cs\">var newUser = new User { Email = &quot;UserEmail@email.ru&quot;, IsBlocked = false, };  bool isValid=true; string errors = RepositoryContainer.Instance.Resolve&lt;UsersRepository&gt;().ValidateAndReturnErrorString(newUser, out isValid); if (!isValid) { \tMessageBox.Show(errors, &quot;Error..&quot;, MessageBoxButtons.OK, MessageBoxIcon.Error); } <\/code><\/pre>\n<p>  <\/p>\n<h5>\u0414\u0435\u043c\u043e \u043f\u0440\u043e\u0435\u043a\u0442<\/h5>\n<p>  \u041f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0431\u0440\u0430\u0442\u044c \u043d\u0430 \u044f\u043d\u0434\u0435\u043a\u0441 \u0434\u0438\u0441\u043a\u0435 <a href=\"http:\/\/yadi.sk\/d\/P9XDDznpMj6p8\">http:\/\/yadi.sk\/d\/P9XDDznpMj6p8<\/a>.<br \/>  \u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0447\u0442\u043e \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0421\u0423\u0411\u0414 MSSQL.<br \/>  \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f MSSQL Express, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0441   <\/p>\n<pre><code class=\"xml\"> &lt;value&gt;Data Source=.\\; Initial Catalog=EFDemoApp; Integrated Security=True; Connection Timeout=5&lt;\/value&gt; <\/code><\/pre>\n<p>  \u043d\u0430   <\/p>\n<pre><code class=\"xml\"> &lt;value&gt;Data Source=.\\SQLEXPRESS; Initial Catalog=EFDemoApp; Integrated Security=True; Connection Timeout=5&lt;\/value&gt; <\/code><\/pre>\n<p>  <\/p>\n<h5>\u041f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0435<\/h5>\n<p>  \u0412\u0435\u0441\u044c \u0432\u044b\u0448\u0435\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u044d\u0442\u043e \u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u041e\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c, \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c, \u043d\u043e \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0443\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0442 \u0441 \u0443\u0441\u043f\u0435\u0445\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432.<br \/>  \u0412 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u044f \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u043b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u0441\u0438\u043b \u043d\u0430 \u0442\u043e \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0438 \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e \u043c\u043e\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c\u0438.<\/p>\n<p>  \u0412\u0441\u0435\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e!    \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/219947\/\"> http:\/\/habrahabr.ru\/post\/219947\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/3b3\/256\/4a8\/3b32564a8b65eeecbf6cc9240a1dd00b.png\"\/><\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e!<\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0442\u043e\u043f\u0438\u043a\u0435 \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0441\u043b\u043e\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c (Data Access Level) \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a Entity Framework-\u0443, \u0434\u0430\u043b\u0435\u0435 EF, \u043e \u0442\u043e\u043c \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0442\u043e\u044f\u043b\u0438 \u0438 \u043a\u0430\u043a \u044f \u0438\u0445 \u0440\u0435\u0448\u0438\u043b. \u0412\u0435\u0441\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u0434 \u0438\u0437 \u043f\u043e\u0441\u0442\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 <a href=\"http:\/\/yadi.sk\/d\/P9XDDznpMj6p8\">\u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u043d\u044b\u0439 \u0434\u0435\u043c\u043e \u043f\u0440\u043e\u0435\u043a\u0442 <\/a> \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0434 \u043b\u0438\u0431\u0435\u0440\u0430\u043b\u044c\u043d\u043e\u0439 \u043b\u0438\u0446\u0435\u043d\u0437\u0438\u0435\u0439 MIT, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043a\u0430\u043a \u0432\u0430\u043c \u0443\u0433\u043e\u0434\u043d\u043e.<br \/>  \u0421\u0440\u0430\u0437\u0443 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u043d\u0443\u0442\u044c, \u0447\u0442\u043e \u0432\u0435\u0441\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 2-\u0445 \u043b\u0435\u0442 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043a\u0440\u0443\u043f\u043d\u043e\u0439 \u0440\u043e\u0441\u0441\u0438\u0439\u0441\u043a\u0438\u0439 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438, \u043d\u043e \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0432\u044b\u0441\u043e\u043a\u043e\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c.<\/p>\n<p>  \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-219947","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/219947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=219947"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/219947\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=219947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=219947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=219947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}