I want to persist some data on a database using Entity Framework.
I have some bigger POCOs but I want to store some of the properties only.
I know that I can achieve this with the Fluent API
by using the Ignore()
method. But is there also the possibility of not only ignoring a defined property but all properties but the defined?
So if you have a POCO like this:
public class MyPoco
{
public int Id { get; set; }
public string Name { get; set; }
.
.
.
public int SomeSpecialId { get; set; }
}
And you only want to store the Id
and the SomeSpecialId
, you would do:
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity<MyPoco>().Ignore(x => x.Name);
builder.Entity<MyPoco>().Ignore(x => x.WhatEver);
.
.
.
// ignore everything but Id and SomeSpecialId
base.OnModelCreating(builder);
}
Problem now is that if you have to extend the POCO but don't want to persist those extended properties you also have to change the OnModelCreating()
method. So is there a way of doing something like:
public override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity<MyPoco>().IgnoreAllBut(x => x.Id, x.SomeSpecialId);
base.OnModelCreating(builder);
}