0

I'm trying to do simplest thing in EF, to save data record into database table.

Everything goes well, model gets created, added, and saved, but when I go to the table the is nothing...

Does anybody see what I'm doing wrong?

This is my DbContext:

public class AuthContext : IdentityDbContext<ApplicationUser>
{
    public AuthContext() : base("AuthContext")
    {
    }

    public virtual DbSet<TransferResponse> Transactions { get; set; }
    public virtual DbSet<FailCounter> Fails { get; set; } 
}

This is my model:

public class FailCounter
{
    [Key]
    public int Id { get; set; }
    public string UserId { get; set; }
    public int FailCounterValue { get; set; }
}

And this is the code for saving:

using(AuthContext dbcnt = new AuthContext())
{
    var userId = repo.FindByUserName(model.DeviceId);
    FailCounter failCount = new FailCounter();
    failCount.FailCounterValue = 1;
    failCount.UserId = userId.Id;
    dbcnt.Fails.Add(failCount);
    dbcnt.SaveChanges();
    return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid username or password.");
}

I've used debugger and found nothing... userId is not null, so that line is not a problem. No exception, nothing, but then again database is empty.

11
  • By "going to the table" do you mean looking at it in VS via the database tools or selecting values in your code? If the first: Maybe you're just looking at the wrong database an EF is using a different ConnectionString. Commented Apr 16, 2015 at 9:23
  • @germi both... I've even look it from SQL Managment Studio... And during debugging I've checked if the values were correct, too :-/ Commented Apr 16, 2015 at 9:24
  • Could your code be pointing to a 'test' database by mistake (for example, a (localdb)\v11.0 database)? Commented Apr 16, 2015 at 9:25
  • Run SQL Profiler to see what SQL statements are executed. What is the return value of SaveChanges? Are you using a transaction that didn't get commited? Commented Apr 16, 2015 at 9:25
  • 1
    @PanagiotisKanavos I've just did, and it's been saving values all the time. First I've restarted SQL MS, than I've restarted VS, still nothing, after I've restarted my machine all data was there... Totally weird situation, but it works after all. Thanks guys! Commented Apr 16, 2015 at 9:27

1 Answer 1

1

try this instead of FailCounter failCount = new FailCounter();
try it like that FailCounter failCount = dbcnt.Fails.Create();

Sign up to request clarification or add additional context in comments.

1 Comment

well, nice if it works, but Add should do it also.

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.