Upgrade to EF 6.1.1 makes effect of [NotMapped] disappear
Asked Answered
B

5

9

I have upgraded from EF 4.3.1 to 6.1.1 and now it seems like the annotation [NotMapped] is useless. Yes, i have change to correct assembly and everything looks fine on compilation.

Everywhere where [NotMapped] is present the property is handled as a domain property and i get an error that EF can't find the matching column in database.

Example:

private bool _authenticated = true;

        [NotMapped]
        public bool Authenticated
        {
            get { return _authenticated; }
            set { _authenticated = value; }
        }

Yes, it seems like i can work around this by adding...

 modelBuilder.Entity<User>().Ignore(x => x.Authenticated);

...but then, whats the use of [NotMapped] in EF6?

(Worked perfect before upgrade)

Buhler answered 3/9, 2014 at 14:18 Comment(2)
Is there some code around where you're encountering the exception?Hie
Everywhere i do a get, update or put to database and the [NotMapped] property i included i get the error: Invalid column name 'Authenticated'. (or whatever the column/property is called). In this case with User object the code is a simple repository call: public virtual T FirstOrDefault(Expression<Func<T, bool>> where) { return _objectSet.FirstOrDefault(where); }Buhler
B
7

Solved by first uninstall and then reinstalling EF on all projects in the solution.

I think it was some mismatch in .NET versions for some projects when i upgraded to EF6 the first time which made the system take the [NotMapped] annotaition from the wrong assembly (.NET instead of EF).

This led me to it: http://social.msdn.microsoft.com/Forums/en-US/2d682be0-daca-45c4-ad76-5885acc6004f/possible-bug-with-inheritance-and-notmapped?forum=adodotnetentityframework

...and most the line: "If you use the new annotations from the System.ComponentModel.DataAnnotations.dll assembly in .NET 4.5 they will not be processed by Code First."

Buhler answered 4/9, 2014 at 9:39 Comment(2)
I'm seeing this issue on my EF 6.1.3 project. My WebAPI's "Put" method gives me a ModelState error on a property I marked NotMapped. Funny, it's only for one of the 3 NotMap properties that I'm seeing the error, however.Billiebilling
In my case, the prop is of type NameValueCollection, and I cannot even use the OnModelCreating fluent api to declare it not mapped - the intellisense is telling me it must be a non-nullable type.Billiebilling
N
3

I also think that there is some mismatch with the .NET version and EF6, which made the program take the [NotMapped] annotation from a wrong assembly.

In particular, the problem is on the use of these two references: System.ComponentModel.DataAnnotations; System.ComponentModel.DataAnnotations.Schema.

I noted that, in this situation, we can not use both reference on same class file, because the NotMapped attribute will be assigned to a different dll of the expected. Even if you assign one of this reference in the code without putting the directive using (putting the complete reference on the attribute declaration, for instance), the program will still have this bug.

To solve this problem, I removed the reference System.ComponentModel.DataAnnotations from the class, leaving only the System.ComponentModel.DataAnnotations.Schema reference to use the NotMapped attribute. And to supply the miss of the first reference (form validation actions), I implemented the validation on the client-side (using jquery + javascript).

using System;
using System.Collections.Generic;
//using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public partial class Account
{       

    //[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "O campo nome é obrigatório!")]
    //[System.ComponentModel.DataAnnotations.StringLength(50, ErrorMessage = "O campo nome deve possuir no máximo 50 caracteres!")]
    //[System.ComponentModel.DataAnnotations.Display(Name = "Nome")]
    public string Name { get; set; }

    //[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "O campo nome é obrigatório!")]
    //[System.ComponentModel.DataAnnotations.StringLength(100, ErrorMessage = "O campo email deve possuir no máximo 100 caracteres!")]
    //[System.ComponentModel.DataAnnotations.Display(Name = "Email")]
    public string Email { get; set; }

    //[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "O campo senha é obrigatório!")]
    //[System.ComponentModel.DataAnnotations.Display(Name = "Senha")]
    //[System.ComponentModel.DataAnnotations.DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
    [NotMapped]
    public string Password { get; set; }

    //[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "O campo confirmação de senha é obrigatório!")]
    //[System.ComponentModel.DataAnnotations.Display(Name = "Confirmação da senha")]
    //[System.ComponentModel.DataAnnotations.DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
    //[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "A confirmação da senha está diferente da senha informada.")]
    [NotMapped]
    public string ConfirmPassword { get; set; }
Normanormal answered 1/6, 2015 at 18:28 Comment(0)
C
2

Late to the party, but I had this case:

using System.ComponentModel.DataAnnotations;//I needed this for [Key] attribute on another property
using System.ComponentModel.DataAnnotations.Schema;//this one is for [NotMapped]
...
[ScriptIgnore]
[NotMapped]
public System.Timers.Timer Timer { get; set; }

This would generate outrageous stuff like:

AddColumn("dbo.mytable", "Timer_AutoReset", c => c.Boolean(nullable: false));
AddColumn("dbo.mytable", "Timer_Enabled", c => c.Boolean(nullable: false));
AddColumn("dbo.mytable", "Timer_Interval", c => c.Double(nullable: false));

Experimenting with this I came to the conclusion that [NotMapped] is ignored if there is another attribute on the column. If it is possible - in my case it was - remove it and [NotMapped] won't be ignored.

Coons answered 13/2, 2018 at 21:36 Comment(1)
Just encountered the same issue. Looks like this is a bug.Platinocyanide
S
0

The issue I had was with an out-dated .EDMX file (created to speed up the application startup time). It was generated prior to the [NotMapped] property being added so the property so the "NotMapped" property was included.

Regenerating the .EDMX file after adding the NotMapped attribute fixed the issue.

Susceptive answered 26/2, 2020 at 15:25 Comment(0)
P
-1

I think the NotMapped has to be the first attribute above the element. This works if not then it doesn't work.

Picard answered 14/9, 2023 at 10:0 Comment(3)
This is simply not true. Please only post answers of which you're convinced they're correct. Not "I think".Carbaugh
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Imes
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewProvitamin

© 2022 - 2024 — McMap. All rights reserved.