1

I use entity framework code first along with OData controllers in spa application, for my composite tables (forexample : Personnel -> PayRolls) i have added two properties in partial class with NotMapped attribute. since i use them later on save changes. i check whether a record has been deleted or modified on the client side or not to save in database.

the partial class is:

 public partial class PayRoll
{
    [NotMapped]
    public bool IsDirty { get; set; }

    [NotMapped]
    public bool IsDeleted { get; set; }
}

if i don't add NotMapped attribute during loading data i got sql exception which shows Invalid column name 'IsDirty'.\r\nInvalid column name 'IsDeleted . so i add NotMapped attribute to make it understand they are not database' table fields.

but during saving data i got error which shows : The property 'IsDirty' does not exist on type 'Template.CodeFirst.PayRoll'. Make sure to only use property names that are defined by the type . It worked fine with entity framework database first(.edmx file) though.

the Put method on oDataController is :

 // PUT: odata/Hrm_Personnels(5)
     [CheckKey("hrm.Personnels.Edit")]
    public IHttpActionResult Put([FromODataUri] int key, Personnel personnel)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (key != personnel.Id)
        {
            return BadRequest();
        }

        foreach (var payroll in personnel.PayRolls)
        {
            if (payroll.IsDirty)

                db.Entry(payroll).State = (payroll.ID > 0) ? EntityState.Modified : EntityState.Added;

           if (payroll.IsDeleted)

                db.Entry(payroll).State = EntityState.Deleted;
        }

        db.SaveChanges();

        db.Entry(personnel).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!PersonnelExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(personnel);
    }

thanks in advance

3
  • 1
    Try to Ignore(x => x.IsDirty) it explicitly in onModelCreating of your context. Commented Mar 4, 2015 at 9:05
  • I wonder if your problem is something to do with how partial classes are serialized? Commented Mar 4, 2015 at 10:53
  • since i used code first approach , i added IsDirty and IsDeleted properties to the original class not partial class as above sample code, but still it doesn't make any difference and still i get error. Commented Mar 4, 2015 at 11:22

1 Answer 1

-1

Actually the first comment is the correct answer and by using the code below and removing NotMapped attributes from the IsDirty and IsDeleted properties the problem solved.

 modelBuilder.Entity<PayRoll>().Ignore(c => c.IsDirty);
        modelBuilder.Entity<PayRoll>().Ignore(c => c.IsDeleted);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.