EntityFramework CreatedOn属性只能在SaveChanges方法中设置

时间:2016-01-02 11:41:53

标签: c# .net entity-framework entity-framework-6 readonly

我有一个名为.login-box button{ width: 100%; height: 40px; background-color: #ffd133; color: #ffffff; text-transform: uppercase; font-size: 14px; font-weight: bold; border: 1px solid #ffd133; border-radius: 5px; cursor: pointer; } .add-category-box button{ width: 48%; height: 40px; background-color:#ffd133; color: #ffffff; text-transform: uppercase; font-size: 14px; font-weight: bold; border: 1px solid #ffd133; border-radius: 5px; cursor: pointer; } 的模型对象,我将它用作我的基类。

Entity

然后我重写了[Serializable] public class Entity { [Key] public int Id { get; set; } [Required] public DateTime? ModifiedOn { get; set; } [Required] public DateTime? CreatedOn { get; set; } } 这样的方法,所以每次创建新记录SaveChanges属性都会自动设置。

CreatedOn

我的问题是如何防止在其他地方设置public override int SaveChanges() { ObjectContext context = ((IObjectContextAdapter)this).ObjectContext; foreach (var entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)) { var entity = entry.Entity as Entity; if(entry.State == EntityState.Added) { entity.CreatedOn = DateTime.UtcNow; } entity.ModifiedOn = DateTime.UtcNow; } return base.SaveChanges(); } CreatedOn属性,ModifiedOn方法除外。例如,不应该允许这样做:

SaveChanges

1 个答案:

答案 0 :(得分:1)

有两种方法可以达到这个目的 1)使您的DbContext成为嵌套类(因此您可以访问外部类的私有字段)
2)使用反射(如@Hamlet Hakobyan建议的那样)

示例1

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
namespace ConsoleApplication3
{
   class Program
   {
       public static void Main()
       {
            Entity.MyContext context = new Entity.MyContext();
            context.Entities.Add(new Entity());
            // context.Entities.Add(new Entity() { Created = DateTime.Now }); //Does not compile
            context.SaveChanges();
        }
   }

    [Serializable]
    public class Entity
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public DateTime? Created { get; private set; }

        public class MyContext : DbContext
        {
            public DbSet<Entity> Entities { get; set; }

            public override int SaveChanges()
            {
                ObjectContext context = ((IObjectContextAdapter)this).ObjectContext;
                foreach (var entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
                {
                    var entity = entry.Entity as Entity;
                    if (entry.State == EntityState.Added)
                        entity.Created = DateTime.Now;
                }
                return base.SaveChanges();
            }
        }
    }
}

示例2

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;

namespace ConsoleApplication3
{
    class Program
    {
        public static void Main()
        {
            MyContext context = new MyContext();
            context.Entities.Add(new Entity()); // CreatedOn = DateTime.Now does not compile
            context.SaveChanges();
            Console.ReadLine();
        }
    }

    public class MyContext : DbContext
    {
        public DbSet<Entity> Entities { get; set; }

        public override int SaveChanges()
        {
            ObjectContext context = ((IObjectContextAdapter)this).ObjectContext;
            foreach (var entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
            {
                var entity = entry.Entity as Entity;
                if (entry.State == EntityState.Added)
                    entity.GetType().GetProperty("CreatedOn").SetValue(entity, DateTime.Now);
            }
            return base.SaveChanges();
        }
    }

    [Serializable]
    public class Entity
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public DateTime? CreatedOn { get; private set; }
   }
}