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

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
ali
  • 1,023
  • 2
  • 14
  • 38
  • 1
    Try to `Ignore(x => x.IsDirty)` it explicitly in `onModelCreating` of your context. – haim770 Mar 04 '15 at 09:05
  • I wonder if your problem is something to do with how partial classes are serialized? – Colin Mar 04 '15 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. – ali Mar 04 '15 at 11:22

1 Answers1

-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);
ali
  • 1,023
  • 2
  • 14
  • 38