zoukankan      html  css  js  c++  java
  • EntityFramework Core 学习笔记 —— 包含与排除属性

    原文地址:[https://docs.efproject.net/en/latest/modeling/included-properties.html][1]

    在模型中包含一个属性意味着 EF 拥有了这个属性的元数据并且将尝试在数据库中进行读写属性的值。

    内容导航

    • [约定][2]
    • [Data Annotation][3]
    • [Fluent API][4]

    约定

    根据约定,带有 getter 和 setter 的公共属性将被包含在模型中。

    Data Annotations

    我们可以使用 Data Annotaions 来从模型中排除一个属性。
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    
        [NotMapped]    // 人工高亮
        public DateTime LoadedFromDatabase { get; set; }
    }
    

    Fluent API

    我们可以使用 Fluent API 来从模型中排除一个属性。 ``` class MyContext : DbContext { public DbSet Blogs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>().Ignore(b => b.LoadedFromDatabase);    // 人工高亮
    }
    

    }

    public class Blog
    {
    public int BlogId { get; set; }
    public string Url { get; set; }

    public DateTime LoadedFromDatabase { get; set; }
    

    }

    
    
      [1]: https://docs.efproject.net/en/latest/modeling/included-properties.html
      [2]:#1
      [3]:#2
      [4]:#3
  • 相关阅读:
    Game Engine Architecture 3
    Game Engine Architecture 2
    补码
    工厂模式
    Game Engine Architecture 1
    YDWE Keynote
    3D Math Keynote 4
    3D Math Keynote 3
    3D Math Keynote 2
    OGRE中Any 类型的实现
  • 原文地址:https://www.cnblogs.com/JacZhu/p/5734850.html
Copyright © 2011-2022 走看看