Upgrading to EF 6.1.1 causes [NotMapped] to disappear

I upgraded from EF 4.3.1 to 6.1.1, and now it seems that the [NotMapped] annotation is useless. Yes, I have changes in the correct assembly, and everything looks good when compiled.

Wherever [NotMapped] is present, the property is treated as a domain property, and I get an error that EF cannot find the corresponding column in the database.

Example:

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

Yes, it looks like I can get around this by adding ...

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

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

(Upgraded to update)

+5
source share
3 answers

It is solved by the first uninstallation, and then reinstalls EF for all projects in the solution.

I think this was some inconsistency in the .NET versions for some projects when I first upgraded to EF6, which caused the system to accept [NotMapped] annotations from the wrong assembly (.NET instead of EF).

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

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

+5
source

I also believe that there is some inconsistency with the versions of .NET and EF6, which caused the program to take the [NotMapped] annotation from the wrong assembly.

In particular, the problem is using these two links: System.ComponentModel.DataAnnotations; System.ComponentModel.DataAnnotations.Schema.

I noted that in this situation we cannot use both links in the same class file, because the NotMapped attribute will be assigned to another dll expected. Even if you assign one of these links in code without specifying a directive using (for example, to completely reference an attribute declaration), the program will still have this error.

To solve this problem, I removed the System.ComponentModel.DataAnnotations class reference from the class, leaving only the System.ComponentModel.DataAnnotations.Schema link to use the NotMapped attribute. And to provide the missing first link (form validation actions), I performed a client-side validation (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; } 
+2
source

Late to the party, but I had this occasion:

 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 will create outrageous things 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] ignored if there is another attribute in the column. If it is possible - in my case it was - delete it, and [NotMapped] will not be ignored.

0
source

Source: https://habr.com/ru/post/1201742/


All Articles