Search code examples
c#model-view-controllerpocopartial-classes

How to extend POCO generated by Reverse POCO Generator?


I used Reverse POCO Generator to generate pocos for my project (partial classes) .

Then, I extended one of my pocos by creating a new partial class (new file) with the same class name and in the same namespace. I added new property, for example:

public int NewProperty { get; set; }

Now, the problem is, when I query database, I see that new property is included in my DBContext. I see select statement like: SELECT Field1, Field2, NewProperty FROM MyDatabase.

And of course, I get following error: SqlClientException: Invalid column name 'NewProperty' as this field doesn't exist.

How can I stop DBContext from including my new property in database operations? I did this on my previous projects and had no issues (used edmx models).

Thanks


Solution

  • If you're using Entity Framework, you can make it ignore a property by adding the NotMapped attribute, like so:

    [NotMapped]
    public int NewProperty { get; set; }
    

    This can also be set in fluent API if you prefer, it should be something like this for EF Core:

    modelBuilder.Entity<YourClass>().Ignore(t => t.NewProperty);