EF6-CodeFirst: Excluding non persisted fields

Posts   
 
    
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 26-Apr-2017 08:20:06   

If I add in code only fields/properties to a partial entity model class and then try to persist the entity EF6 complains that the property doesn't exist in the database. Is there a way to: A) exclude the field in the generated DB context? B) set a flag that EF6 only persists fields which have been explicitly set? C) create these fields in the designer as code only?

Thanks! Patrick

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 26-Apr-2017 10:34:54   

More of an EF question, but I think this is the answer to your question: http://stackoverflow.com/questions/10572442/how-to-ignore-a-property-when-using-entity-framework-code-first

(so add that attribute to the properties you've added manually to the code so EF will skip them)

Frans Bouma | Lead developer LLBLGen Pro
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 26-Apr-2017 18:34:23   

Ah thanks I'm aware that there is an attribute but since the model is used in a .NET 3.5 & .NET 4.6.2 context we can't use an attribute that was introduced in .NET 4.5.

So the only way to do it would be to it add it to the data context in CodeFirst. The only overwrite I can find is DataContext.OnContextCreated though which gets called after the context is already created. So it looks like I would need another extensibility method in the template so I can define the ignore's in a partial class.

modelBuilder.Entity<TheModelAffected>().Ignore(t => t.TheIgnoredProperty);

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Apr-2017 21:20:18   

Looking for your feedback if the template change worked out well with you, so others having the same issue can benefit from this thread.

pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 27-Apr-2017 17:36:04   

I'm sure it would help if a template change was made but as much as I know this hasn't happened yet:

So it looks like I would need another extensibility method in the template so I can define the ignore's in a partial class (ie at the point after the model is initialized but before it completed building.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 28-Apr-2017 10:14:16   

pat wrote:

Ah thanks I'm aware that there is an attribute but since the model is used in a .NET 3.5 & .NET 4.6.2 context we can't use an attribute that was introduced in .NET 4.5.

So the only way to do it would be to it add it to the data context in CodeFirst. The only overwrite I can find is DataContext.OnContextCreated though which gets called after the context is already created. So it looks like I would need another extensibility method in the template so I can define the ignore's in a partial class.

modelBuilder.Entity<TheModelAffected>().Ignore(t => t.TheIgnoredProperty);

Thanks!

No, there's an easier way, simply create a derived class from the Context and ModelBuilder classes: in the derived class of the ModelBuilder, you override each mapping method for an entity and first call the base' version and then make your changes to the EntityTypeBuilder<T> object passed in.

In the derived class of the Context class, override OnModelCreating and do:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    new YourModelBuilderDerivedClass().BuildModel(modelBuilder);
}

where YourModelBuilderDerivedClass is your modelbuilder subclass.

Frans Bouma | Lead developer LLBLGen Pro
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 28-Apr-2017 21:29:37   

Cool that works thank you