Upgrade to EF 6.1.1 makes effect of [NotMapped] disappear

asp.net-mvc c# entity-framework-4 entity-framework-6

Question

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)

1
8
9/3/2014 2:18:30 PM

Accepted Answer

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."

6
9/4/2014 9:39:44 AM

Popular Answer

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; }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow