开发者

Simple Entity Framework 4 adding object

We have an MVC application that creates entity models and stores them in a session. Later on, we want to commit these to the database. Just trying to do a

db.Attendees.AddObject(attendee);

throws the error

The EntityKey property can only be set when the current value of the property is null.

The entity key is just a simple type of long and is an identity column in the database. What are we missing? This seems like such a simple thing to do? In order to get things to work, we have had to create a copy of the object and then save the copy. Can you not put entity framework models into session, bring them back out, and then save them?

Here's the Attendee description in the Entity Framework model...it is hitting a SQL Server 2008 DB

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

    /// <summary>
    /// Create a new Attendee object.
    /// </summary>
    /// <param name="attendeeID">Initial value of the AttendeeID property.</param>
    /// <param name="firstName">Initial value of the FirstName property.</param>
    /// <param name="lastName">Initial value of the LastName property.</param>
    /// <param name="email">Initial value of the Email property.</param>
    /// <param name="createdBy">Initial value of the CreatedBy property.</param>
    /// <param name="createdOn">Initial value of the CreatedOn property.</param>
    /// <param name="modifiedBy">Initial value of the ModifiedBy property.</param>
    /// <param name="modifiedOn">Initial value of the ModifiedOn property.</param>
    pu开发者_如何学编程blic static Attendee CreateAttendee(global::System.Int64 attendeeID, global::System.String firstName, global::System.String lastName, global::System.String email, global::System.String createdBy, global::System.DateTime createdOn, global::System.String modifiedBy, global::System.DateTime modifiedOn)
    {
        Attendee attendee = new Attendee();
        attendee.AttendeeID = attendeeID;
        attendee.FirstName = firstName;
        attendee.LastName = lastName;
        attendee.Email = email;
        attendee.CreatedBy = createdBy;
        attendee.CreatedOn = createdOn;
        attendee.ModifiedBy = modifiedBy;
        attendee.ModifiedOn = modifiedOn;
        return attendee;
    }

    #endregion
    #region Primitive Properties

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


You should be adding, not attaching, the new objects.

Update If you get the same error when you AddObject, then you need to make sure the StoreGeneratedPattern in SSDL is set to Identity. The designer should do this for you if your DB is set up correctly and your provider supports it.


If the item does not exist in the database you do not need to call context.Attach, only context.AddObject or context.<collection>.Add.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜