如何不先持久化属性 EF4 代码?

发布于 2024-09-16 18:37:46 字数 382 浏览 2 评论 0原文

如何使用 codefirst EF4 创建非持久属性?

MS说有一个StoreIgnore属性,但我找不到它。

http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx

有吗一种使用 EntityConfiguration 进行设置的方法?

How do I make non persisted properties using codefirst EF4?

MS says there is a StoreIgnore Attribute, but I cannot find it.

http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx

Is there a way to set this up using EntityConfiguration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷离° 2024-09-23 18:37:46

在 EF Code-First CTP5 中,您可以使用 [NotMapped] 注释。

using System.ComponentModel.DataAnnotations;
public class Song
{
    public int Id { get; set; }
    public string Title { get; set; }

    [NotMapped]
    public int Track { get; set; }

In EF Code-First CTP5, you can use the [NotMapped] annotation.

using System.ComponentModel.DataAnnotations;
public class Song
{
    public int Id { get; set; }
    public string Title { get; set; }

    [NotMapped]
    public int Track { get; set; }
秋风の叶未落 2024-09-23 18:37:46

目前,我知道有两种方法可以做到这一点。

  1. 将“dynamic”关键字添加到属性中,这会阻止映射器保留它:

    私人性别 性别;
    公共动态 性别
    {
        获取{返回性别; }
        设置{性别=值; }
    }
    
  2. 重写 DBContext 中的 OnModelCreating 并重新映射整个类型,忽略您不想保留的属性:< /p>

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        Base.OnModelCreating(modelBuilder);
        modelBuilder.Entity().MapSingleType(p => new { p.FirstName, ... });
    }         
    

使用方法 2,如果 EF 团队引入了 Ignore,您将能够轻松地将代码更改为:

     modelBuilder.Entity<Person>().Property(p => p.IgnoreThis).Ignore();

Currently, I know of two ways to do it.

  1. Add the 'dynamic' keyword to the property, which stops the mapper persisting it:

    private Gender gender;
    public dynamic Gender
    {
        get { return gender; }
        set { gender = value; }
    }
    
  2. Override OnModelCreating in DBContext and remap the whole type, omitting the properties you don't want to persist:

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Person>().MapSingleType(p => new { p.FirstName, ... });
    }         
    

Using method 2, if the EF team introduce Ignore, you will be able to easily change the code to:

     modelBuilder.Entity<Person>().Property(p => p.IgnoreThis).Ignore();
情绪操控生活 2024-09-23 18:37:46

如果您不想使用注释,可以使用 流畅的API。重写 OnModelCreating 并使用 DbModelBuilder 的 Ignore() 方法。假设您有一个“歌曲”实体:

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Song>().Ignore(p => p.PropToIgnore);
    }
}

您还可以使用 EntityTypeConfiguration 将配置移动到单独的类以获得更好的可管理性:

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new SongConfiguration());
    }

}

public class SongConfiguration : EntityTypeConfiguration<Song>
{
    public SongConfiguration()
    {
        Ignore(p => p.PropToIgnore);
    }
}

If you don't want to use Annotations, you can use the Fluent API. Override the OnModelCreating and use DbModelBuilder's Ignore() method. Supposing you have a 'Song' entity:

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Song>().Ignore(p => p.PropToIgnore);
    }
}

You can also use EntityTypeConfiguration to move configurations to separate classes for better manageability:

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new SongConfiguration());
    }

}

public class SongConfiguration : EntityTypeConfiguration<Song>
{
    public SongConfiguration()
    {
        Ignore(p => p.PropToIgnore);
    }
}
和我恋爱吧 2024-09-23 18:37:46

我不确定这是否可用。

在此 MSDN 页面 描述了忽略属性和 API,但在下面的评论中,有人于 2010 年 6 月 4 日写道:

您将能够忽略下一个 Code First 版本中的属性,

I'm not sure if this is available yet.

On this MSDN page the Ignore Attribute and API are described but below, in the comments, somebody writes on 4 june 2010:

You will be able to ignore properties in the next Code First release,

段念尘 2024-09-23 18:37:46

添加
使用 System.ComponentModel.DataAnnotations.Schema
到模型类。 (必须包含“SCHEMA”)

将 [NotMapped] 数据注释添加到您想要保留的字段(即不保存到数据库)。

这将阻止它们作为列添加到数据库中的表中。

请注意 - 以前的答案可能包含这些位,但它们没有完整的“使用”子句。他们只是放弃了“模式”——在该模式下定义了 NotMapped 属性。

Add
using System.ComponentModel.DataAnnotations.Schema
to the model class. (Must include "SCHEMA")

Add [NotMapped] data annotation to the field(s) you want to keep from persisting (ie. not save to database).

This will prevent them from being added as a column to the table in the db.

Please note - previous answers may have included these bits, but they did not have the full "using" clause. They merely left off "schema" - under which the NotMapped attribute is defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文