Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NotMapped] Attribute causes a cast error #105

Closed
Letsion opened this issue Sep 29, 2023 · 2 comments
Closed

[NotMapped] Attribute causes a cast error #105

Letsion opened this issue Sep 29, 2023 · 2 comments

Comments

@Letsion
Copy link
Contributor

Letsion commented Sep 29, 2023

[NotMapped] Attribute causes a cast error with the MapIgnoreAnotationHandler.

Fix:
Write your Own Anotation Handler for NotMappedAttribute:

using System.ComponentModel.DataAnnotations.Schema;
using Detached.Mappers;
using Detached.Mappers.Annotations;
using Detached.Mappers.Types.Class;

namespace BOB.Backend.Common.AttributeHandlers
{
    public class NotMappedAnnotationHandler : AnnotationHandler<NotMappedAttribute>
    {
        public override void Apply(NotMappedAttribute annotation, MapperOptions mapperOptions, ClassType typeOptions, ClassTypeMember memberOptions)
        {
            memberOptions.Ignore(true);
        }
    }
}
@leonardoporro
Copy link
Owner

Hi Nico,
Yup, there was a missing handler over there.. removed in 7.1.2.
I thought we agreed not to stop mapping for [NotMapped] but only for [MapIgnore].

A better workardound would be options.AnnotationHandlers.Remove(typeof(NotMappedAttribute));

Anyway, you already know how to create your own handlers, so, excellent..

@Letsion
Copy link
Contributor Author

Letsion commented Oct 4, 2023

The problem was, it should still map the properties with NotMapped, which are not in the database.
Now when I remove NotMapped from the AnnotationHandlers Dictornary, it wants to load and track the Ef Core, but they are not in the database = error.
Suggested solution:
For the NotMapped attribute, map the values from the DTo to the entity, but don't try to load/track them from the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants