Entity Framework transform exclude database table columns
I have a table in a database named product
and it has many columns:
- Id
- Name
- Price
- The size
- Colour
- ....
- ..
I want to create a class to represent this table with only 3 columns. And I will look like this.
public class ProductConfiguration : EntityTypeConfiguration<Product>
{
public ProductConfiguration()
{
ToTable("product");
Property(p => p.ProductId).HasColumnName("id");
Property(p => p.Name).HasColumnName("name");
Property(p => p.Price).HasColumnName("price");
}
}
How can I exclude database columns from the mapping class?
+3
source to share
1 answer
as per the accepted answer and a similar question / answer
public class Product
{
public int Id{ set; get; }
public string Name{ set; get; }
public int Price{ set; get; }
[NotMapped]
public int size{ set; get; }
}
Also you can do it Fluent API by overriding OnModelCreating function in your context class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TheModelAffected>().Ignore(t => t.size);
base.OnModelCreating(modelBuilder);
}
Also this is a good article for mapping properties in EF
Hope it helps.
-1
source to share