忽略Entity Framework 4.1代码中的类属性 [英] Ignoring a class property in Entity Framework 4.1 Code First

查看:282
本文介绍了忽略Entity Framework 4.1代码中的类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,在目前在CTP中的EF 5之前, [NotMapped] 属性不可用,所以我们无法在生产中使用它。

My understanding is that the [NotMapped] attribute is not available until EF 5 which is currently in CTP so we cannot use it in production.

如何将EF 4.1中的属性标记为忽略?

How can I mark properties in EF 4.1 to be ignored?

更新:我注意到其他奇怪的东西。我得到 [NotMapped] 属性工作,但由于某些原因,EF 4.1仍然在数据库中创建一个名为Disposed的列,即使 public bool Disposed {get;私人集合} 标有 [NotMapped] 。当然,该类实现了 IDisposeable ,但是我看不出应该如何。任何想法?

UPDATE: I noticed something else strange. I got the [NotMapped] attribute to work but for some reason, EF 4.1 still creates a column named Disposed in the database even though the public bool Disposed { get; private set; } is marked with [NotMapped]. The class implements IDisposeable of course but I don't see how that should matter. Any thoughts?

推荐答案

您可以使用 NotMapped 注释来指示代码 - 首先排除特定属性

You can use the NotMapped Annotation to instruct Code-First to exclude a particular property

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 命名空间中。

[NotMapped] attribute is included in the System.ComponentModel.DataAnnotationsnamespace.

您也可以使用流利的API 覆盖 DBContext 类中的OnModelCreating 函数类:

You can alternatively do this with Fluent API overriding OnModelCreating function in your DBContext class:

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

http://msdn.microsoft.com/en-us/library/hh295847(v = vs103).aspx

我检查的版本是 EF 4.3 ,这是您使用NuGet时最新的稳定版本。

The version I checked is EF 4.3, which is the latest stable version available when you use NuGet.

这篇关于忽略Entity Framework 4.1代码中的类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆