mercredi 4 mars 2015

Entity framework code first custom properties


Vote count:

0




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



asked 1 min ago

ali

289






Entity framework code first custom properties

Aucun commentaire:

Enregistrer un commentaire