无视C首先实体框架4

无视C首先实体框架4

本文介绍了无视C首先实体框架4.1 $ C $类​​属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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还是创建一个名为设置在数据库列即使公共BOOL处置{获得;私定; } 标记为[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 注释指示code-首先要排除的特定属性

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.DataAnnotations namespace.

您也可以做到这一点与流利的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=vs.103).aspx

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

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

这篇关于无视C首先实体框架4.1 $ C $类​​属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:27