2010-09-06 164 views
2

我的實體中有對象項目。 我創建的項目:在實體框架中保存編輯的對象

public static void Add(Project project) 
     { 
      var context = new Entities(); 
      context.AddToProjects(project); 

      context.SaveChanges(); 
     } 

接下來,我得到這個對象:

public static Project GetProjectById(int idProject) 
     { 
      var context = new Entities(); 
      return context.Projects.Where(p => p.IDProject == idProject).First(); 
     } 

而且這兩種方法很好地工作。

但是,當我想保存編輯的任務:

public static void SaveProject(Project project) 
     { 
      var context = new Entities(); 
      project.EntityKey = new EntityKey("Entities.Projects", "IDProject", project.IDProject); 
      context.Attach(project); 
      context.SaveChanges(); 
     } 

沒有例外方法exectues。但在數據庫中沒有任何變化我必須改變什麼?

這是我enity:

[EdmEntityTypeAttribute(NamespaceName="GWDModel", Name="Project")] 
    [Serializable()] 
    [DataContractAttribute(IsReference=true)] 
    public partial class Project : EntityObject 
    { 
     #region Factory Method 

    /// <summary> 
    /// Create a new Project object. 
    /// </summary> 
    /// <param name="iDProject">Initial value of the IDProject property.</param> 
    /// <param name="name">Initial value of the Name property.</param> 
    /// <param name="expectedCost">Initial value of the ExpectedCost property.</param> 
    /// <param name="status">Initial value of the Status property.</param> 
    public static Project CreateProject(global::System.Int32 iDProject, global::System.String name, global::System.Decimal expectedCost, StatusOfProjectWrapper status) 
    { 
     Project project = new Project(); 
     project.IDProject = iDProject; 
     project.Name = name; 
     project.ExpectedCost = expectedCost; 
     project.Status = StructuralObject.VerifyComplexObjectIsNotNull(status, "Status"); 
     return project; 
    } 

    #endregion 
    #region Primitive Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Int32 IDProject 
    { 
     get 
     { 
      return _IDProject; 
     } 
     set 
     { 
      if (_IDProject != value) 
      { 
       OnIDProjectChanging(value); 
       ReportPropertyChanging("IDProject"); 
       _IDProject = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("IDProject"); 
       OnIDProjectChanged(); 
      } 
     } 
    } 
    private global::System.Int32 _IDProject; 
    partial void OnIDProjectChanging(global::System.Int32 value); 
    partial void OnIDProjectChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String Name 
    { 
     get 
     { 
      return _Name; 
     } 
     set 
     { 
      OnNameChanging(value); 
      ReportPropertyChanging("Name"); 
      _Name = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("Name"); 
      OnNameChanged(); 
     } 
    } 
    private global::System.String _Name; 
    partial void OnNameChanging(global::System.String value); 
    partial void OnNameChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Decimal ExpectedCost 
    { 
     get 
     { 
      return _ExpectedCost; 
     } 
     set 
     { 
      OnExpectedCostChanging(value); 
      ReportPropertyChanging("ExpectedCost"); 
      _ExpectedCost = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("ExpectedCost"); 
      OnExpectedCostChanged(); 
     } 
    } 
    private global::System.Decimal _ExpectedCost; 
    partial void OnExpectedCostChanging(global::System.Decimal value); 
    partial void OnExpectedCostChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.DateTime> PlannedStart 
    { 
     get 
     { 
      return _PlannedStart; 
     } 
     set 
     { 
      OnPlannedStartChanging(value); 
      ReportPropertyChanging("PlannedStart"); 
      _PlannedStart = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("PlannedStart"); 
      OnPlannedStartChanged(); 
     } 
    } 
    private Nullable<global::System.DateTime> _PlannedStart; 
    partial void OnPlannedStartChanging(Nullable<global::System.DateTime> value); 
    partial void OnPlannedStartChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.DateTime> PlannedEnd 
    { 
     get 
     { 
      return _PlannedEnd; 
     } 
     set 
     { 
      OnPlannedEndChanging(value); 
      ReportPropertyChanging("PlannedEnd"); 
      _PlannedEnd = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("PlannedEnd"); 
      OnPlannedEndChanged(); 
     } 
    } 
    private Nullable<global::System.DateTime> _PlannedEnd; 
    partial void OnPlannedEndChanging(Nullable<global::System.DateTime> value); 
    partial void OnPlannedEndChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.DateTime> DateOfStart 
    { 
     get 
     { 
      return _DateOfStart; 
     } 
     set 
     { 
      OnDateOfStartChanging(value); 
      ReportPropertyChanging("DateOfStart"); 
      _DateOfStart = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("DateOfStart"); 
      OnDateOfStartChanged(); 
     } 
    } 
    private Nullable<global::System.DateTime> _DateOfStart; 
    partial void OnDateOfStartChanging(Nullable<global::System.DateTime> value); 
    partial void OnDateOfStartChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.DateTime> DateOfEnd 
    { 
     get 
     { 
      return _DateOfEnd; 
     } 
     set 
     { 
      OnDateOfEndChanging(value); 
      ReportPropertyChanging("DateOfEnd"); 
      _DateOfEnd = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("DateOfEnd"); 
      OnDateOfEndChanged(); 
     } 
    } 
    private Nullable<global::System.DateTime> _DateOfEnd; 
    partial void OnDateOfEndChanging(Nullable<global::System.DateTime> value); 
    partial void OnDateOfEndChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] 
    [DataMemberAttribute()] 
    public Nullable<global::System.Decimal> CurrentCost 
    { 
     get 
     { 
      return _CurrentCost; 
     } 
     set 
     { 
      OnCurrentCostChanging(value); 
      ReportPropertyChanging("CurrentCost"); 
      _CurrentCost = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("CurrentCost"); 
      OnCurrentCostChanged(); 
     } 
    } 
    private Nullable<global::System.Decimal> _CurrentCost; 
    partial void OnCurrentCostChanging(Nullable<global::System.Decimal> value); 
    partial void OnCurrentCostChanged(); 

    #endregion 
    #region Complex Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmComplexPropertyAttribute()] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
    [XmlElement(IsNullable=true)] 
    [SoapElement(IsNullable=true)] 
    [DataMemberAttribute()] 
    public StatusOfProjectWrapper Status 
    { 
     get 
     { 
      _Status = GetValidValue(_Status, "Status", false, _StatusInitialized); 
      _StatusInitialized = true; 
      return _Status; 
     } 
     set 
     { 
      OnStatusChanging(value); 
      ReportPropertyChanging("Status"); 
      _Status = SetValidValue(_Status, value, "Status"); 
      _StatusInitialized = true; 
      ReportPropertyChanged("Status"); 
      OnStatusChanged(); 
     } 
    } 
    private StatusOfProjectWrapper _Status; 
    private bool _StatusInitialized; 
    partial void OnStatusChanging(StatusOfProjectWrapper value); 
    partial void OnStatusChanged(); 

    #endregion 

    #region Navigation Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [XmlIgnoreAttribute()] 
    [SoapIgnoreAttribute()] 
    [DataMemberAttribute()] 
    [EdmRelationshipNavigationPropertyAttribute("GWDModel", "FK_ProjectChanges_Projects", "ProjectChanges")] 
    public EntityCollection<ProjectChanx> ProjectChanges 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<ProjectChanx>("GWDModel.FK_ProjectChanges_Projects", "ProjectChanges"); 
     } 
     set 
     { 
      if ((value != null)) 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<ProjectChanx>("GWDModel.FK_ProjectChanges_Projects", "ProjectChanges", value); 
      } 
     } 
    } 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [XmlIgnoreAttribute()] 
    [SoapIgnoreAttribute()] 
    [DataMemberAttribute()] 
    [EdmRelationshipNavigationPropertyAttribute("GWDModel", "ProjectManagers", "Users")] 
    public EntityCollection<User> Users 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<User>("GWDModel.ProjectManagers", "Users"); 
     } 
     set 
     { 
      if ((value != null)) 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<User>("GWDModel.ProjectManagers", "Users", value); 
      } 
     } 
    } 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [XmlIgnoreAttribute()] 
    [SoapIgnoreAttribute()] 
    [DataMemberAttribute()] 
    [EdmRelationshipNavigationPropertyAttribute("GWDModel", "FK_Milestones_Projects", "Milestone")] 
    public EntityCollection<Milestone> Milestones 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Milestone>("GWDModel.FK_Milestones_Projects", "Milestone"); 
     } 
     set 
     { 
      if ((value != null)) 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Milestone>("GWDModel.FK_Milestones_Projects", "Milestone", value); 
      } 
     } 
    } 

    #endregion 
} 

回答

1

你有兩種選擇。首選是在GetProjectById和SaveProject之間共享上下文。比你的實體將跟蹤更改,你不需要打電話附加。如果你想/需要爲每個操作打開新的上下文,你必須告訴上下文Project已被修改。當實體附加到上下文時,它被跟蹤爲未更改。您必須手動更改實體狀態以在附加後通過調用ObjectStateManager進行修改:

context.ObjectStateManager.ChangeObjectState(project, EntityState.Modified); 

Btw。 ObjectContext是一次性的,所以你應該通過使用或try/finally來處理它。

編輯:

基於你對WCF首選評論是不可能的。但是您也可以檢查自我跟蹤實體(僅限EF 4.0)。

+0

FYI ,ChangeObjectState方法僅在EF 4.0之後纔可用。 @phenevo,ChangeObjectState是一個非常方便的方法,但請注意,它將所有屬性標記爲已更改 - 如果您正在執行最佳併發處理,則可能會造成問題。 – VinayC 2010-09-06 12:57:46

1

這取決於你如何檢索項目進行編輯。如果按照您的GetProjectById方法獲取它,那麼該項目實體正在通過相關ObjectContext跟蹤它的更改。

MSDN

實體框架跟蹤改變附加到一個ObjectContext的對象。實體數據模型工具生成一對名爲OnPropertyChanging和OnPropertyChanged的部分方法。這些方法在屬性設置器中調用。

基本上,所有你需要做的更改保存到一個跟蹤實體呼籲,你從它提取同一目標的背景下SaveChanges()方法。

我建議尋找使用存儲庫模式來管理您的ObjectContexts,以便您有正確的ObjectContext跟蹤和保存更改。

+0

我將這個對象翻譯爲datacontract(WCF),當datacontract支持時,我將它翻譯爲具有相同屬性的Project的新實例,因此跟蹤在這種情況下不起作用,不謝謝您回覆:) – user278618 2010-09-06 06:17:33

3

EF上下文跟蹤實體更改 - 因此如果您要分離/附加實體更改不會被跟蹤。現在,當您嘗試通過附加到上下文來更新實體時,從上下文角度來看,實體沒有任何更改。所以SaveChanges不會嘗試保存任何東西。在調用SaveChanges方法之前,您必須以某種方式將對象標記爲髒或更改。請參閱this blog post來實現它。這裏的yet another post概述了類似的解決方案。