nemo_87
nemo_87

Reputation: 4791

Entity Framework DbContext won't save data into database

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.

Upvotes: 0

Views: 1083

Answers (1)

Sherif Ahmed
Sherif Ahmed

Reputation: 1946

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

Upvotes: 1

Related Questions