zoukankan      html  css  js  c++  java
  • EF(EF Core)中的NotMappedAttribute(转载)

    NotMapped特性可以应用到EF实体类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列。。
    NotManpped特性打破了这个约定,你可以使用NotMapped特性到某个属性上面,然后Code-First就不会为这个属性在数据表中创建列了。
    我们看一下下面的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EF2
    {
        [Table("StudentMaster",Schema="WaHaHa")]
       public class Student
        {
            [Key]
            [Column(Order=5)]
            public int StudentKey1 { get; set; }
    
            [Key]
            [Column(Order=6)]
            public int StudentKey2 { get; set; }
    
            
            [MaxLength(20)]
            [ConcurrencyCheck]
            [Required]
            [Column("SName",Order=1,TypeName="nvarchar")]
            public string StudentName { get; set; }
    
            [NotMapped()]
            public int? Age { get; set; }
    
    
            public int StandardRefId { get; set; }
    
            [ForeignKey("StandardRefId")]
            public virtual Standard Standard { get; set; }
    
          
    
        }
    }

    注意到没有,这个表里面没有Age列。


    但是如果属性,只有Get属性访问器,或者只有set属性访问器,那么Ef Code-First就不会为它创建数据列了。
    请看:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EF2
    {
        [Table("StudentMaster",Schema="WaHaHa")]
       public class Student
        {
            [Key]
            [Column(Order=5)]
            public int StudentKey1 { get; set; }
    
            [Key]
            [Column(Order=6)]
            public int StudentKey2 { get; set; }
    
            
            [MaxLength(20)]
            [ConcurrencyCheck]
            [Required]
            [Column("SName",Order=1,TypeName="nvarchar")]
            public string StudentName { get; set; }
    
            [NotMapped()]
            public int? Age { get; set; }
    
    
            public int StandardRefId { get; set; }
    
    
            public string FirstName 
            {
                get { return FirstName; }
            }
    
            public int myAge;
            public int MyAge 
            {
                set { value = myAge; }
            }
    
            [ForeignKey("StandardRefId")]
            public virtual Standard Standard { get; set; }
    
    
    
          
    
        }
    }

    得到的数据库还是这个:

    NotMappedAttribute无效解决办法

    可以通过NotMappedAttribute标记模型某个属性可以使该属性不必映射到数据库。

    public class Unicorn
    {
    public int Id { get; set; }
    [NotMapped]
    public string Name { get; set; }
    
    [Timestamp]
    public byte[] Version { get; set; }
    
    public int PrincessId { get; set; } // FK for Princess reference
    public virtual Princess Princess { get; set; }
    }

    NotMapped无效的时候,在DbContext的OnModelCreating方法重载中实现

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //不映射到数据库中
        modelBuilder.Entity<BlogArticle>().Ignore(p => p.Title); 
    }

    EF Core中Ignore方法的用法如下:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BlogArticle>(entity =>
        {
            //不映射到数据库中
            entity.Ignore(p => p.Title);
        });
    }

    转载文章:

    数据注解特性--NotMapped

    EF 解除属性映射到数据库中 NotMappedAttribute无效解决办法

  • 相关阅读:
    Leetcode: Rotate Image
    Leetcode: Longest Palindromic Substring && Summary: Palindrome
    Leetcode: Reverse Nodes in k-Group
    Leetcode: Substring with Concatenation of All Words
    Leetcode: Merge k Sorted List
    Summary: Java中函数参数的传递
    Leetcode: Generate Parentheses
    超级wifi
    路由器中继(repeater)模式 和 AP+WDS模式区别?
    route 的标志位
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/10177062.html
Copyright © 2011-2022 走看看