【问题标题】:How not persist property EF4 code first?如何不先保留属性 EF4 代码?
【发布时间】:2011-04-05 10:56:08
【问题描述】:

如何使用 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 进行设置?

【问题讨论】:

  • +1 非常好的问题。
  • 这有什么更新吗?我正在使用发布了 EF4.0 的 VS2010,但我仍然找不到 StoreIgnoreAttribute。掉了吗?

标签: c# .net entity-framework entity-framework-4 ef4-code-only


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    如果您不想使用注解,可以使用Fluent API。覆盖 OnModelCreating 并使用 DbModelBuilder 的 Ignore() 方法。假设您有一个“歌曲”实体:

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

    您还可以将 EntityTypeConfiguration 用于move configurations to separate classes 以获得更好的可管理性:

    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);
        }
    }
    

    【讨论】:

      【解决方案3】:

      在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; }
      

      【讨论】:

      • 如果我尝试使用该属性,则会收到编译时错误。没有定义的。我们需要什么参考和“使用”?
      • 您可能需要添加对 System.ComponentModel.DataAnnotations.dll 的引用并使用 System.ComponentModel.DataAnnotations;我用代码示例更新了我的帖子。
      • 我在第一次引用 System.ComponentModel.DataAnnotations.dll 然后添加对 EntityFramework.dll 的引用时遇到了一个错误。无法解析 NotMapped 属性。删除并重新添加 System.ComponentModel.DataAnnotations.dll 引用解决了该问题。发生在 VS2010 SP1 中。
      • 我使用了[System.ComponentModel.DataAnnotations.Schema.NotMapped],这似乎成功了。
      【解决方案4】:

      目前,我知道两种方法。

      1. 将“动态”关键字添加到属性中,这会阻止映射器对其进行持久化:

        private Gender gender;
        public dynamic Gender
        {
            get { return gender; }
            set { gender = value; }
        }
        
      2. 在 DBContext 中覆盖 OnModelCreating 并重新映射整个类型,省略您不想保留的属性:

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

      使用方法2,如果EF团队引入Ignore,你就可以很方便的把代码改成:

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

      【讨论】:

      • 我的回答已被@Matt 取代。
      【解决方案5】:

      我不确定这是否可用。

      MSDN page 上描述了忽略属性和 API,但在下面,在 cmets 中,有人在 2010 年 6 月 4 日写道:

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

      【讨论】:

      • 哇,这在代码优先的故事中似乎是一个巨大的整体。如果代码优先实体不能将数据作为属性包含在数据库中的非直接行中,那么它们将很难构成一个丰富的域模型。
      • 问题是 Code first 还不是 RTM 而只是 CTP 所以目前它只用于评估而不是用于生产。
      • 同时,创建额外的“属性”作为方法而不是属性,因为这些不是持久的。然后,您可以继续针对适当的对象开发代码。一旦 StoreIgnore 属性可用,就可以轻松更改回属性并更新所有引用。
      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多