栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

忽略Entity Framework 4.1 Code First中的类属性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

忽略Entity Framework 4.1 Code First中的类属性

您可以使用

NotMapped
属性数据注释来指示Code-First排除特定属性

public class Customer{    public int CustomerID { set; get; }    public string FirstName { set; get; }     public string LastName{ set; get; }     [NotMapped]    public int Age { set; get; }}

[NotMapped]
属性包含在
System.ComponentModel.DataAnnotations
名称空间中。

您也可以在类中使用

Fluent API
覆盖
OnModelCreating
函数来执行此操作
DBContext

protected override void onModelCreating(DbModelBuilder modelBuilder){   modelBuilder.Entity<Customer>().Ignore(t => t.LastName);   base.onModelCreating(modelBuilder);}

http://msdn.microsoft.com/zh-
CN/library/hh295847(v=vs.103).aspx

我检查的版本是

EF 4.3
,这是使用NuGet时可用的最新稳定版本。


编辑2017年9月

Asp.NET Core(2.0)

数据注解

如果您正在使用asp.net core( 在撰写本文时为2.0 ),则该

[NotMapped]
属性可以在属性级别上使用。

public class Customer{    public int Id { set; get; }    public string FirstName { set; get; }     public string LastName { set; get; }     [NotMapped]    public int FullName { set; get; }}

流利的API

public class SchoolContext : DbContext{    public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)    {    }    protected override void onModelCreating(ModelBuilder modelBuilder)    {        modelBuilder.Entity<Customer>().Ignore(t => t.FullName);        base.onModelCreating(modelBuilder);    }    public DbSet<Customer> Customers { get; set; }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470725.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号