Can u tell me what is the problem?
-
1what do you mean "is not working"? what is the exception? what EF version are you using? how do you pass the `db` variable to the `Customer` entity and to the click handler in the aspx page? – Liviu Mandras Jul 09 '14 at 07:28
-
1can you tell us what the error is ? My Crystal ball is in for service – phil soady Jul 09 '14 at 07:28
-
I am not getting an error. it's not save the values – PsyGnosis Jul 09 '14 at 07:37
-
db variable in the same class like entity db = new entity(); and also aspx page has different db variable. I think its not working because of that – PsyGnosis Jul 09 '14 at 07:39
3 Answers
If you are using two different instances of the DbContext
(the db
variable as you named it) then nothing will be saved when you call SaveChanges
on a context different than the one where your entities are tracked. You need to use the Attach method first.
db.customer_images.Attach(item);
db.SaveChanges();
However I think in your case you can avoid the attach step if you refactor a bit you code and don't use the DbContext from the entity itself.

- 6,540
- 2
- 41
- 65
Before going through my answer, you must check, if you are attaching the item as shown in excepted answer or check this code:.
if (dbStudentDetails != null && dbStudentDetails.Id != 0)
{
// update scenario
item.Id = dbStudentDetails.Id;
_context.Entry(dbStudentDetails).CurrentValues.SetValues(item);
_context.Entry(dbStudentDetails).State = EntityState.Modified;
}
else
{
// create scenario
_context.StudentDetails.Add(item);
}
await _context.SaveChangesAsync();
If above solution doesn't work, then check the below answer.
Saw a very wired issue, and thought to must answer this. as this can be a major issue if you have lots of constraints and indexes in your SQL.
db.SaveChanges() wasn't throwing any error, but not working (I have tried Exception or SqlException). This was not working because the Unique constraint was not defined properly while creating the Entity Models.
How you can Identified the issue:
- I connected my SQL Server and opened the SQL Profiler.
- Just before the db.SaveChanges(), I cleared all my profiler logs and ran the db.SaveChanges(). It logged the statement. I copied the script from the profiler and ran the script in SQL Server.
- And bingo, I can see the actual error, which is being thrown at SQL Server side.
(images: have some hints, how you can get the execute statement from Profiler and run on sql server)
What you can do For Entity Framework Core:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Students>().HasIndex(p => new { p.RollNumber, p.PhoneNumber }).IsUnique(true).IsClustered(false).HasDatabaseName("IX_Students_Numbers");
}
What you can do For Entity Framework 6 and below:
using System.Data.Entity.ModelConfiguration;
internal partial class StudentsConfiguration : EntityTypeConfiguration<Students>
{
public StudentsConfiguration()
{
HasIndex(p => new { p.RollNumber, p.PhoneNumber }).IsUnique(true).IsClustered(false).HasName("IX_Students_Numbers");
}
}

- 3,265
- 1
- 26
- 29
Try to query your entity by Id, eg:
entity = this.repo.GetById(item.id);
entity.is_front = false;
if (dbSaveChanges() > 0)
{
....
}

- 39
- 1
- 3