2011-06-12 57 views
0

我將對象插入List並保存。如何更新實體

表臺(INT汽車incriment ID, VARCHAR NAME)

var desk = new Desk() 
      { 
      name = "newName" 
      }; 

m_RoomsContext.Desks.Add(desk); 
m_RoomsContext.SubmitChanges(); 

//desk.id == 0

我需要得到ID書桌。這個怎麼做?

物業編號:

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

EDMX:

<EntityType Name="Desk"> 
      <Key> 
      <PropertyRef Name="id" /> 
      </Key> 
      <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> 
      <Property Name="width" Type="float" Nullable="false" /> 
      <Property Name="height" Type="float" Nullable="false" /> 
      <Property Name="x" Type="int" Nullable="false" /> 
      <Property Name="y" Type="int" Nullable="false" /> 
      <Property Name="countMax" Type="int" Nullable="false" /> 
      <Property Name="countReal" Type="int" /> 
      <Property Name="date" Type="datetime" /> 
      <Property Name="id_status" Type="int" Nullable="false" /> 
     </EntityType> 
+0

是否在實體模型中使用'StoreGeneratedPattern.Identity'定義的Id(檢查EDMX文件中'Id'屬性的屬性)? – 2011-06-12 10:30:50

+0

我更新了帖子。不存在StoreGeneratedPattern.Identity。 – Mediator 2011-06-12 11:00:20

回答

3

後提交看看desk.ID屬性=>將被分配從數據庫中新的價值。

+0

我覺得這個。但不更新ID。爲什麼? – Mediator 2011-06-12 09:16:19