标签:mod day protected add err property pre 排序 rri
一、隐藏属性是指:在实体类中未定义,但在数据库中有字段定义,隐藏属性不支持通过数据注解的方式配置
例子:数据库表中添加LastUpdated,数据库模型中无LastUpdated字段
1.重写OnModelCreating添加配置
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Blog>().Property<DateTime>("LastUpdated"); base.OnModelCreating(modelBuilder); }
2.隐藏属性数据的添加
Blog blog = new Blog { Url = "XXXX" }; _context.Entry(blog).State = EntityState.Added; _context.Entry(blog).Property("LastUpdated").CurrentValue = DateTime.Now.AddDays(1); _context.SaveChanges();
3.隐藏属性查询条件、排序
var blogs = _context.Blogs.Where(b => EF.Property<DateTime>(b, "LastUpdated") > DateTime.Now); var blogs2 = _context.Blogs .OrderBy(b => EF.Property<DateTime>(b, "LastUpdated"));
4.隐藏属性的修改
Blog blog = _context.Blogs.Find(1); _context.Entry(blog).Property("LastUpdated").CurrentValue = DateTime.Now; blog.Url = "xxxxx"; _context.SaveChanges();
标签:mod day protected add err property pre 排序 rri
原文地址:https://www.cnblogs.com/Adoni/p/12296133.html