2014-09-03 109 views
5

我從EF 4.3.1升級到6.1.1效果,現在看來似乎註釋[NotMapped]是沒用的。是的,我已經改變了正確的彙編,編譯時一切都很好。升級到6.1.1 EF使得[NotMapped]消失

到處其中[NotMapped]存在於屬性作爲域屬性處理,我得到一個錯誤的是EF找不到在數據庫中的匹配列。

例子:

private bool _authenticated = true; 

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

是的,好像我可以解決此加...

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

......不過呢,有什麼用的[NotMapped]在EF6 ?

(升級前曾任職完美)

+0

是否有一些代碼在你遇到異常的地方? – 2014-09-03 14:20:53

+0

無處不在我得到,更新或放到數據庫和我包括的[NotMapped]屬性我得到錯誤:無效的列名稱'Authenticated'。 (或者不管列/屬性被調用)。在這種情況下使用用戶對象的碼是一個簡單的存儲庫調用:公共實際的t FirstOrDefault(表達式>其中) { 返回_objectSet.FirstOrDefault(其中); } – Gruffalon 2014-09-04 06:09:05

回答

5

解決先卸載,然後在溶液中的所有項目重新安裝EF。

我認爲這是一些項目.NET版本中的某些不匹配,當我第一次升級到EF6,使系統從錯誤的程序集(.NET而不是EF)獲取[NotMapped] annotaition時。

這使我吧:http://social.msdn.microsoft.com/Forums/en-US/2d682be0-daca-45c4-ad76-5885acc6004f/possible-bug-with-inheritance-and-notmapped?forum=adodotnetentityframework

...最行:「如果你在.NET 4.5 使用新 註釋從System.ComponentModel.DataAnnotations.dll組裝他們不會由Code First處理。「

+0

我在EF 6.1.3項目中看到了這個問題。我的WebAPI的「Put」方法在我標記爲NotMapped的屬性上給我一個ModelState錯誤。有趣的是,它只針對3 * NotMap *屬性中的一個,但是我看到了這個錯誤。 – bkwdesign 2017-02-09 10:55:10

+0

在我的情況下,prop屬於'NameValueCollection'類型,我甚至不能使用'OnModelCreating'流利的api聲明它沒有映射 - intellisense告訴我它必須是非空類型。 – bkwdesign 2017-02-09 11:01:24

2

我也覺得有一些不匹配與.NET版本和EF6,這使得程序從一個錯誤的組裝取[NotMapped]註釋。

特別地,該問題是在使用這兩個參考文獻的:System.ComponentModel.DataAnnotations; System.ComponentModel.DataAnnotations.Schema。

我注意的是,在這種情況下,我們不能用在同一個類文件中同時引用,因爲NotMapped屬性將被分配到不同的DLL預期的。即使您在代碼中指定了此引用中的一個,但未將指令使用(例如在屬性聲明中放置完整引用),該程序仍然會存在此錯誤。

要解決此問題,我從類中刪除了引用System.ComponentModel.DataAnnotations,只留下System.ComponentModel.DataAnnotations.Schema引用以使用NotMapped屬性。爲了提供第一個引用(表單驗證操作)的遺漏,我在客戶端實現了驗證(使用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; } 
0

遲到了,但我有這種情況:

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

這個做實驗我得出的結論是[NotMapped]是:

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

喜歡這將產生離譜的東西如果列中有另一個屬性,則忽略它。如果有可能 - 在我的情況下是 - 刪除它,並且[NotMapped]不會被忽略。