无视C首先实体框架4.1 $ C $类​​属性实体、框架、属性

2023-09-02 11:45:30 作者:爱情这出戏〃我没演技

我的理解是,[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.

 
精彩推荐
图片推荐