2014-07-25 75 views
0

我正在使用Entity Framework 6(Model First)。所以我有幾個類是由model.tt生成的。這是我的汽車類:將PropertyChanged.Fody添加到Entity Framework Model.tt

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated from a template. 
// 
//  Manual changes to this file may cause unexpected behavior in your application. 
//  Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace MyNamespace 
{ 

    using System; 
    using System.Collections.Generic; 

    public partial class Car 
    { 
     public Car() 
     { 
      this.Wheels = new HashSet<Wheel>(); 
     } 

     public int CarId { get; set; } 
     public string Make { get; set; } 
     public string Model { get; set; } 
     public string Year { get; set; } 
     public string VIN { get; set; } 

     public virtual ICollection<Wheel> Wheels { get; set; } 
    } 
} 

我也在我的項目中的其他類上使用PropertyChanged.Fody。我有幾個類,但只是從我生成的類包裝性能,像這樣的屬性:

using System; 
using PropertyChanged; 

namespace MyNamespace 
{ 
    [ImplementPropertyChanged] 
    public class CarWrapper 
    { 

     public CarWrapper(Car car) 
     { 
      Car = car; 
     } 

     public Car car { get; set; } 

     public string Make 
     { 
      get { return Car.Make; } 
      set { Car.Make = value; } 
     } 

     public string Model 
     { 
      get { return Car.Model; } 
      set { Car.Model = value; } 
     }  

     public string Year 
     { 
      get { return Car.Year; } 
      set { Car.Year = value; } 
     } 

     public string VIN 
     { 
      get { return Car.VIN; } 
      set { Car.VIN = value; } 
     } 
    } 
} 

所以,ProperyChanged.Fody會做它的魔力在我車內財物,而不是其他人,但如果我是編輯我的Model.tt並添加[ImplementPropertyChanged]屬性,我生成的類將全部通知屬性更改。然後,我可以修改車內財物的CarWrapper像這樣:

[AlsoNotifyFor("Make")] 
[AlsoNotifyFor("Model")] 
[AlsoNotifyFor("Year")] 
[AlsoNotifyFor("VIN")] 
public Car car { get; set; } 

這將是在所有的,如果我要得到通知的內車內財物的變化做一件好事嗎?它會是多餘的嗎?還有其他建議嗎?

回答

0

我最終將[ImplementsPropertyChanged]添加到我的Car類的其他部分類。這使我可以控制實際需要實現屬性更改的類。

儘管將它添加到.tt文件似乎不會導致問題,但將它添加到部分類的另一半可以實現更多控制。