Evitar la asignación de propiedades con Fluent API
Tengo una clase Product
y un tipo complejo 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
}
¿Es posible evitar la asignación de la propiedad "Country" desde AddressDetails
dentro de la clase Product
? (porque nunca lo necesitaré para la clase Product
)
Algo como esto
Property(p => p.AddressDetails.Country).Ignore();
7 answers
Para EF5 y mayores:
En el DbContext.OnModelCreating
override para su contexto:
modelBuilder.Entity<Product>().Ignore(p => p.AddressDetails.Country);
Para EF6: No tienes suerte. Ver Respuesta del Señor.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:17:29
Desafortunadamente la respuesta aceptada no funciona, al menos con EF6 y especialmente si la clase hija no es una entidad.
No he encontrado ninguna manera de hacer esto a través de fluent API. La única forma en que funciona es a través de anotaciones de datos:
public class AddressDetails
{
public string City { get; set; }
[NotMapped]
public string Country { get; set; }
// other properties
}
Nota: Si tienes una situación en la que Country
debe excluirse solo cuando es parte de cierta otra entidad, entonces no tienes suerte con este enfoque.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-30 02:11:48
Si está utilizando una implementación de EntityTypeConfiguration puede usar el método Ignore:
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");
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-13 15:30:18
En EF6 puede configurar el tipo complejo:
modelBuilder.Types<AddressDetails>()
.Configure(c => c.Ignore(p => p.Country))
De esta manera el País de la propiedad será siempre ignorado.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-04 16:23:59
Prueba esto
modelBuilder.ComplexType<AddressDetails>().Ignore(p => p.Country);
Funcionó para mí en un caso similar.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-29 17:15:16
Aunque me doy cuenta de que esta es una vieja pregunta, las respuestas no resolvieron mi problema con EF 6.
Para EF 6 necesita crear una asignación ComplexTypeConfiguration.
Ejemplo:
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);
}
}
Si su Contexto está cargando configuraciones manualmente, necesita agregar el nuevo ComplexMap, si usa la sobrecarga de FromAssembly, se recogerá con el resto de los objetos de configuración.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-25 15:41:34
También se puede hacer en Fluent API, simplemente agregue en la asignación el siguiente código
Esto.Ignore (t = > t.Country), probado en EF6
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-07 08:29:49