简单实体框架4添加对象

发布于 2024-10-02 13:35:55 字数 3054 浏览 3 评论 0原文

我们有一个 MVC 应用程序,它创建实体模型并将它们存储在会话中。稍后,我们要将这些提交到数据库。只是尝试执行

db.Attendees.AddObject(attendee);

会引发错误

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

实体键只是简单的 long 类型,并且是数据库中的标识列。我们缺少什么?这看起来是一件很简单的事情吗?为了让事情正常工作,我们必须创建对象的副本,然后保存该副本。你不能将实体框架模型放入会话中,将它们带出来,然后保存它们吗?

这是实体框架模型中的参加者描述...它正在访问 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>
    public 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();

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>
    public 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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-10-09 13:35:55

您应该添加而不是附加新对象。

更新如果您在AddObject时遇到相同的错误,那么您需要确保SSDL中的StoreGeneratePattern设置为Identity< /代码>。如果您的数据库设置正确并且您的提供商支持它,那么设计者应该为您执行此操作。

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.

追风人 2024-10-09 13:35:55

如果数据库中不存在该项目,则无需调用 context.Attach,只需调用 context.AddObjectcontext..Add

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文