2014-09-03 15 views
5

Tôi đã nâng cấp từ EF 4.3.1 lên 6.1.1 và bây giờ có vẻ như chú thích [NotMapped] là vô ích. Có, tôi đã thay đổi để lắp ráp chính xác và mọi thứ có vẻ tốt về biên dịch.Nâng cấp lên EF 6.1.1 có hiệu lực của [NotMapped] biến mất

Ở mọi nơi mà [NotMapped] có mặt thuộc tính được xử lý như một thuộc tính miền và tôi gặp phải lỗi mà EF không thể tìm thấy cột phù hợp trong cơ sở dữ liệu.

Ví dụ:

private bool _authenticated = true; 

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

Vâng, nó có vẻ như tôi có thể làm việc xung quanh này bằng cách thêm ...

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

... nhưng sau đó, whats việc sử dụng [NotMapped] trong EF6 ?

(Làm việc hoàn hảo trước khi nâng cấp)

+0

Có một số mã xung quanh nơi bạn đang gặp phải ngoại lệ không? –

+0

Ở khắp mọi nơi tôi thực hiện việc nhận, cập nhật hoặc đưa vào cơ sở dữ liệu và thuộc tính [NotMapped] i kèm theo, tôi nhận được lỗi: Tên cột không hợp lệ 'Authenticated'. (hoặc bất kỳ cột/thuộc tính nào được gọi). Trong trường hợp này với đối tượng User, mã là một cuộc gọi kho đơn giản: public virtual T FirstOrDefault (Biểu thức > trong đó) { return _objectSet.FirstOrDefault (ở đâu); } – Gruffalon

Trả lời

5

giải quyết bằng cách gỡ bỏ cài đặt đầu tiên và sau đó cài đặt lại EF trên tất cả các dự án trong dung dịch.

Tôi nghĩ rằng đó là một số không khớp trong phiên bản .NET cho một số dự án khi tôi nâng cấp lên EF6 lần đầu tiên khiến hệ thống nhận chú thích [NotMapped] từ lắp ráp sai (.NET thay vì EF).

này dẫn tôi đến nó: http://social.msdn.microsoft.com/Forums/en-US/2d682be0-daca-45c4-ad76-5885acc6004f/possible-bug-with-inheritance-and-notmapped?forum=adodotnetentityframework

... và hầu hết các dòng: "Nếu bạn sử dụng chú thích mới từ lắp ráp System.ComponentModel.DataAnnotations.dll trong .NET 4.5 họ sẽ không được xử lý theo Mã đầu tiên. "

+0

Tôi thấy vấn đề này trong dự án EF 6.1.3 của tôi. Phương thức "Đặt" của WebAPI cho tôi một lỗi ModelState trên thuộc tính mà tôi đã đánh dấu là NotMapped. Hài hước, nó chỉ cho _one_ của 3 * NotMap * tài sản mà tôi thấy lỗi, tuy nhiên. – bkwdesign

+0

Trong trường hợp của tôi, prop là loại 'NameValueCollection', và tôi thậm chí không thể sử dụng' OnModelCreating' fluent api để khai báo nó không được ánh xạ - intellisense cho tôi biết nó phải là một kiểu không nullable. – bkwdesign

2

Tôi cũng nghĩ rằng có một số không phù hợp với phiên bản .NET và EF6, làm cho chương trình nhận chú thích [NotMapped] từ một assembly sai.

Cụ thể, sự cố là về việc sử dụng hai tham chiếu sau: System.ComponentModel.DataAnnotations; System.ComponentModel.DataAnnotations.Schema.

Tôi lưu ý rằng, trong trường hợp này, chúng tôi không thể sử dụng cả hai tham chiếu trên cùng một tệp lớp, vì thuộc tính NotMapped sẽ được gán cho một dll khác của dự kiến. Ngay cả khi bạn chỉ định một tham chiếu này trong mã mà không đặt chỉ thị bằng cách sử dụng (đặt tham chiếu hoàn chỉnh vào khai báo thuộc tính), chương trình sẽ vẫn có lỗi này.

Để giải quyết vấn đề này, tôi đã xóa tham chiếu System.ComponentModel.DataAnnotations khỏi lớp, chỉ để tham chiếu System.ComponentModel.DataAnnotations.Schema sử dụng thuộc tính NotMapped. Và để cung cấp sự thiếu sót của tham chiếu đầu tiên (các hành động xác thực biểu mẫu), tôi đã thực hiện xác nhận hợp lệ ở phía máy khách (sử dụng 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

muộn để đảng, nhưng tôi đã có trường hợp này:

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

Điều này sẽ tạo ra những thứ kỳ quặc như:

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

Thử nghiệm với điều này tôi đi đến kết luận rằng [NotMapped] là bỏ qua nếu có thuộc tính khác trên cột.Nếu có thể - trong trường hợp của tôi là - xóa nó và [NotMapped] sẽ không bị bỏ qua.

Các vấn đề liên quan