.netcore下EF model没有NotMapped

NotMapped特性可以应用到EF实体类的属性中,如果类属性对应的库字段没有的,可以添加类属性标签[NotMapped],否则出现以下错误:

Unknown column ‘字段名‘ in ‘field list‘ 错误

在.netcore下找不到上述标签是因为 在程序集中要using System.ComponentModel.DataAnnotations.Schema

而这个命名空间在 EntityFramework.dll中 ,没有在System.ComponentModel.DataAnnotations这个dll中

解决办法:

1、可以在库中添加此字段。

2、不想在库中添加新字段,可以在DbContext的OnModelCreating方法重载中实现

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

 

原文地址:https://www.cnblogs.com/lunawzh/p/14777438.html