save object throw exception:Lephone.Data.DataException : To call this functionn, the UnsavedValue must be set.

Topics: Developer Forum, Project Management Forum
Apr 29, 2009 at 1:35 AM
i'm a new guy to use DbEntry, when i define a object like follow:

[Serializable]
[DbTable("Com_Contact")]
public abstract class Contact : DbBaseEntity<Contact, Guid>
{
    [DbKey(IsDbGenerate = false)]
    public abstract Guid ContactId { get; set; }
    [AllowNull, Length(30)]
    public abstract string Suffix { get; set; }
    [AllowNull, Length(30)]
    public abstract string NickName { get; set; }
    [Length(30)]
    public abstract string OfficePhone { get; set; }
    [AllowNull, Length(128)]
    public abstract string OfficeAddress { get; set; }
    [AllowNull, Length(30)]
    public abstract string MobilePhone { get; set; }
    [AllowNull, Length(30)]
    public abstract string SecondPhone { get; set; }
    [AllowNull, Length(30)]
    public abstract string Fax { get; set; }
    [AllowNull, Length(30)]
    public abstract string HomePhone { get; set; }
    [AllowNull, Length(128)]
    public abstract string HomeAddress { get; set; }
    public abstract DateTime? ContactTime { get; set; }
    [AllowNull, Length(6)]
    public abstract string ZipCode { get; set; }
    [AllowNull, Length(255)]
    public abstract string PostalAddress { get; set; }
    [AllowNull, Length(128), StringColumn(Regular = CommonRegular.EmailRegular)]
    public abstract string Email { get; set; }
    [AllowNull, Length(128), StringColumn(Regular = CommonRegular.UrlRegular)]
    public abstract string HomePage { get; set; }
    [AllowNull, Length(128)]
    public abstract string IcqNumber { get; set; }
  
    public Contact() { }
    public Contact(Guid id) : this() {
        ContactId = id;
    }

    public Contact Init(string name, string officePhone, string createdBy)
    {
        this.Name = name;
        this.OfficePhone = officePhone;
        this.CreatedBy = createdBy;
        this.UpdatedBy = UpdatedBy;
        return this;
    }
}
when i use nunit test this entity, throw an exception:Lephone.Data.DataException : To call this functionn, the UnsavedValue must be set.
[Test]
public void Test1()
{
    DbEntry.Delete<Contact>(WhereCondition.EmptyCondition);

    Contact c1 = DynamicObject.NewObject<Contact>();
    c1.Init("JACKY", "22305779", "Admin");
    DbEntry.Save(c1); // here throw exception
}
any body can help me,thanks.

Coordinator
Apr 29, 2009 at 3:25 AM
[Serializable]
[DbTable("Com_Contact")]
public abstract class Contact : DbObjectModel<Contact, Guid>
{
    [AllowNull, Length(30)]
    public abstract string Suffix { get; set; }
    [AllowNull, Length(30)]
    public abstract string NickName { get; set; }
    [Length(30)]
    public abstract string OfficePhone { get; set; }
    [AllowNull, Length(128)]
    public abstract string OfficeAddress { get; set; }
    [AllowNull, Length(30)]
    public abstract string MobilePhone { get; set; }
    [AllowNull, Length(30)]
    public abstract string SecondPhone { get; set; }
    [AllowNull, Length(30)]
    public abstract string Fax { get; set; }
    [AllowNull, Length(30)]
    public abstract string HomePhone { get; set; }
    [AllowNull, Length(128)]
    public abstract string HomeAddress { get; set; }
    public abstract DateTime? ContactTime { get; set; }
    [AllowNull, Length(6)]
    public abstract string ZipCode { get; set; }
    [AllowNull, Length(255)]
    public abstract string PostalAddress { get; set; }
    [AllowNull, Length(128), StringColumn(Regular = CommonRegular.EmailRegular)]
    public abstract string Email { get; set; }
    [AllowNull, Length(128), StringColumn(Regular = CommonRegular.UrlRegular)]
    public abstract string HomePage { get; set; }
    [AllowNull, Length(128)]
    public abstract string IcqNumber { get; set; }
  
    public Contact Init(string name, string officePhone, string createdBy)
    {
        this.Name = name;
        this.OfficePhone = officePhone;
        this.CreatedBy = createdBy;
        this.UpdatedBy = UpdatedBy;
        return this;
    }
}

[Test]
public void Test1()
{
    DbEntry.Delete<Contact>(WhereCondition.EmptyCondition);

    Contact c1 = Contact.New().Init("JACKY", "22305779", "Admin");
    DbEntry.Save(c1); // here throw exception
}

Apr 29, 2009 at 8:22 AM
thanks lifeng reply my question.
the DbBaseEntity is my custom class,

    [Serializable]
    public abstract class DbBaseEntity<T, TKey> : DbObjectBase where T : DbBaseEntity<T, TKey>
    {
        [Length(128)]
        public abstract string Name { get; set; }
        [Length(30)]
        public abstract string CreatedBy { get; set; }
        [SpecialName]
        public abstract DateTime CreatedOn { get; set; }
        [AllowNull, Length(30)]
        public abstract string UpdatedBy { get; set; }
        [SpecialName]
        public abstract DateTime? UpdatedOn { get; set; }
        [SpecialName]
        public abstract int LockVersion { get; set; }
        [AllowNull, Length(255)]
        public abstract string Comment { get; set; }

        public DbBaseEntity<T, TKey> Init(string name) {
            this.Name = name;
            return this;
        }

         public DbBaseEntity<T, TKey> Init(string name, string createdBy)
        {
           this.Name = name;
           this.CreatedBy = createdBy;
           this.UpdatedBy = createdBy;
           return this;
        }
    }