2010-09-06 117 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概述了类似的解决方案。