32

クラスProductと複合型がありますAddressDetails

public class Product
{
    public Guid Id { get; set; }

    public AddressDetails AddressDetails { get; set; }
}

public class AddressDetails
{
    public string City { get; set; }
    public string Country { get; set; }
    // other properties
}

クラスAddressDetails内からの「Country」プロパティのマッピングを防ぐことは可能ですか?(クラスProductでは必要ないので)Product

このようなもの

Property(p => p.AddressDetails.Country).Ignore();
4

7 に答える 7

28

EF5以前の場合:コンテキスト のDbContext.OnModelCreatingオーバーライド:

modelBuilder.Entity<Product>().Ignore(p => p.AddressDetails.Country);

EF6の場合:運が悪いです。Mrchiefの回答を参照してください。

于 2013-02-28T10:50:14.070 に答える
10

If you are using an implementation of EntityTypeConfiguration you can use the Ignore Method:

public class SubscriptionMap: EntityTypeConfiguration<Subscription>
{
    // Primary Key
    HasKey(p => p.Id)

    Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    Property(p => p.SubscriptionNumber).IsOptional().HasMaxLength(20);
    ...
    ...

    Ignore(p => p.SubscriberSignature);

    ToTable("Subscriptions");
}
于 2015-01-13T15:30:18.030 に答える
4

これは古い質問であることは理解していますが、答えはEF 6の問題を解決しませんでした.

EF 6 の場合、ComplexTypeConfiguration マッピングを作成する必要があります。

例:

public class Workload
{
    public int Id { get; set; }
    public int ContractId { get; set; }
    public WorkloadStatus Status {get; set; }
    public Configruation Configuration { get; set; }
}
public class Configuration
{
    public int Timeout { get; set; }
    public bool SaveResults { get; set; }
    public int UnmappedProperty { get; set; }
}

public class WorkloadMap : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Workload>
{
    public WorkloadMap()
    {
         ToTable("Workload");
         HasKey(x => x.Id);
    }
}
// Here This is where we mange the Configuration
public class ConfigurationMap : ComplexTypeConfiguration<Configuration>
{
    ConfigurationMap()
    {
       Property(x => x.TimeOut).HasColumnName("TimeOut");
       Ignore(x => x.UnmappedProperty);
    }
}

Context が構成を手動でロードしている場合は、新しい ComplexMap を追加する必要があります。FromAssembly オーバーロードを使用している場合は、残りの構成オブジェクトと共に取得されます。

于 2016-05-25T15:41:34.043 に答える
2

EF6 では、複合型を構成できます。

 modelBuilder.Types<AddressDetails>()
     .Configure(c => c.Ignore(p => p.Country))

そうすれば、Country プロパティは常に無視されます。

于 2015-06-04T16:23:59.577 に答える
-2

Fluent API でも同様に実行できます。次のコードをマッピングに追加するだけです。

this.Ignore(t => t.Country)、EF6 でテスト済み

于 2015-01-07T08:29:49.443 に答える