【问题标题】:Disable or turn off the auto generation of mappings based on the POCO properties根据 POCO 属性禁用或关闭映射的自动生成
【发布时间】:2013-07-18 01:36:58
【问题描述】:

我正在使用 Code First 构造连接到现有数据库(我知道这在技术上不是 Code First)。我这样做是为了可以使用 LINQ to Entities 而不是 LINQ to SQL 来模拟 Fake Data Repositories。在去年的 CTP3 中,这很有效 - 我可以为我的表创建一个新配置,并显式添加映射到数据库的 POCO 的属性。

2011 年 7 月,Code First 包含在 EF4.1 中,所以我想我会升级。不幸的是,EF 现在对我来说太聪明了。例如,a) 假设我的 POCO 的所有属性都映射到数据库字段,例如,属性 Person.TempPassword{get;set;} 导致生成的 SQL 在我的 Person 表中搜索 TempPassword 列; b) 如果我从我的 POCO 继承一个类(例如,Employee 继承自 Person),EF 开始生成鉴别器列等。

我知道我可以使用 Ignore() 或 [NotMapped] 属性来解决前一个问题,但是这很麻烦,我不想这样做。后一个问题我无法解决。

我所需要的只是一种能够关闭所有这些智能的方法,并让我像以前一样显式设置属性。请注意,我也尝试过删除每一个约束,例如 modelBuilder.Conventions.Remove();

我还重写了对 OnModelCreating() 的调用,而不是调用 base.OnModelCreating(),只是包含了我自己的配置代码,如下所示。

我的配置示例是:

public class AddressBookConfiguration : EntityTypeConfiguration<AddressBook>{
    public AddressBookConfiguration()
    {
    this.HasKey(x => x.AddressBookID);
    this.ToTable("AddressBook");
    this.Property(x => x.AddressBookID);
    this.Property(x => x.AddressBookName);
    this.Property(x => x.WhenCreated);
    this.Property(x => x.TotalItems);
    }
}

谢谢。

【问题讨论】:

    标签: entity-framework ef-code-first


    【解决方案1】:

    当需要扩展实体框架类功能时,我通常采取的方法是从数据库中生成数据模型,而不是使用Extension Methods

    我知道这还不够,您不能以这种方式创建属性。 不过,恐怕这可能是你能得到的最好的了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      相关资源
      最近更新 更多