如何更新实体

时间:2011-06-12 09:09:45

标签: c# entity-framework entity-framework-4

我将对象插入List并保存。

  

Table Desk(int auto 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>

1 个答案:

答案 0 :(得分:3)

提交后查看desk.ID property =&gt;它将从数据库中分配新值。