4

I have a strange problem with entity framework, In my project im using EF 6 codefirst and for my database im using sqlserver 2012

as with a table iam using isactive column of type bit, which represents 0 or 1 only. and defaultly i kept this column value as 1 which is represents true.

When we want to delete a record from the table we should have to make 1 to 0, but the entity framework doesn't updating the value.

is any one having this strange problem any 1 with solution here ??

Code:

 using (var context = new TriviaContext())
            {

                var booking = context.Booking.FirstOrDefault(b => b.BookingId == id);
                context.Entry(booking).Property(u => u.IsActive).IsModified=true;
                booking.IsActive = false;


                context.SaveChanges();

                return Ok(booking);
            }

Booking Model:

class Booking
{
 public int BookingId { get; set; }
 public string Name{get;set;}
 [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
 public bool? IsActive { get; set; }
}

edit: actually i have included DatabaseGenerated Attribute in my model class, may be this is causing the problem ?

TriviaContext:

public class TriviaContext : DbContext
{
    public TriviaContext()
        : base("name=DefaultConnection")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
    public DbSet<Bookings> Booking { get; set; }
}
5
  • Can you show some code, table structure?
    – Danh
    Oct 17, 2016 at 5:45
  • can you show the code ?
    – Sampath
    Oct 17, 2016 at 5:46
  • well the EF doesn't generating the update code ... Oct 17, 2016 at 5:50
  • Can you show the TrivaContext code? This works fine for me Oct 17, 2016 at 9:18
  • added triviaContext code.. Oct 17, 2016 at 10:36

3 Answers 3

2

well i was using [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute for default generate the value , but this is causing me that The Entity Framework dint generating the sql to update the records , so when i remove this attribute the EF able to generating the updating things.so i could go with my work for now. but this kinda coding is right or wrong ... any experienced guys can suggest me for this kinda problems to solve....

Regards BosH

1
  • 3 years later and EF doesn't have a nice way to allow you to let a property be updated by the database AND in code. They need a DatabaseGeneratedOption.Default that lets the database handle if the property is null then set the default, but at the same time lets you update it. What they have now is not intuitive. Is that too much to ask for? Nov 28, 2019 at 20:45
0

Try this:

     using (var context = new TriviaContext())
        {
       var booking  = (from x in context.Booking
         where x.BookingId == id
         select x).First();

            booking.IsActive = false;
            context.SaveChanges();

            return Ok(booking);
        }
0
0

The entity is loaded and tracked, so no need to use the Entry method to change the property value, just change and save the entity

using (var context = new TriviaContext())
            {

                var booking = context.Booking.Find(id);
                booking.IsActive = false;
                context.SaveChanges();

                return Ok(booking);
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.