3

I know there's a lot of questions about it but i've read like 20 of them and couldn't find answer for me. I have this error

"An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

Additional information: An error occurred while updating the entries. See the inner exception for details."

When I go to InnerException it says

"Invalid object name 'dbo.Samochodies'."

. I don't know what the hell is this because i don't have any 'Samochodies' in my program.. Anyway, that's the code:

CarsController.cs

public ActionResult Create([Bind(Include = "Id,Brand,Model,Price,Bought,Sold")] Samochody car)
    {
        if (ModelState.IsValid)
        {
            baza.Cars.Add(car);
            baza.SaveChanges(); //error here
            return RedirectToAction("Index");
        }
        return View(car);
    }

Samochody class

public class Samochody
{
    public int Id { get; set; }
    public string Brand { get; set; }
    public string Model { get; set; }
    public decimal Price { get; set; }
    public DateTime Bought { get; set; }
    public DateTime Sold { get; set; }

    public class CarDBCtxt : DbContext
    {
        public DbSet<Samochody> Cars { get; set; }
    }
9
  • dbo.Samochodies is the table name EF is trying to map in your DB. What is the real name of the Table Commented Dec 15, 2016 at 19:44
  • I didn't specify it, im using Code First (i think? :D) but maybe i am doing something wrong Commented Dec 15, 2016 at 19:48
  • So, are you sure your DB doesn't exist yet? Commented Dec 15, 2016 at 19:50
  • Tutorial guy said that the db file should appear in AppData folder after we will try to acces the db for first time but i see it didn't appear actually.. Commented Dec 15, 2016 at 19:53
  • I think the problem is because your DB already exist, maybe you changed the connection string? Commented Dec 15, 2016 at 19:54

3 Answers 3

4

When you work with an existing DB, if you don't specify the table name with you're mapping your entity, then EF will try to find by convention a table named Samochodies in your DB. One solution could be using Table attribute to specify the real table name:

[Table("YourTableName")]
public class Samochody
{
  //...
}

Now, maybe the exception is because you changed the name of your entity. To case like this EF has some initializers that could help you to solve this kind of issue every time you change the model, in your case it would be:

public class CarDBCtxt : DbContext
{
    public DbSet<Samochody> Cars { get; set; }

    public CarDBCtx(): base("CarDBCtxt") 
    {
      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CarDBCtxt>());  
    }
}

If you want to learn more about initializers, take a look this link.

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

5 Comments

It shows error "Method must have a return type" at CarDbCtx, unexpected token ":" and that DropCreateDatabaseIfModelChanges requires 1 type arguments
Maybe it's because mdf file did't appear in AppData folder? In the tutorial it was there after all this code
I forgot DropCreateDatabaseIfModelChanges is generic
What errors? Please, take a look what is a constructor: msdn.microsoft.com/en-us/library/ace5hbzh.aspx
Please, if you think my answer helped you to solve your issue, consider mark the question as answered.
2

problem was that the Id of the table is not AUTO_INCREMENT,please make Id as AUTO_INCREMENT

Comments

1

This answer helped me from Koskilla :

Modifying my Model to use nullable DateTime -typed objects (Nullable? -type) solved the issue. So, in short, to resolve the error “The conversion of a datetime2 data type to a DateTime data type resulted in an out-of-range value”, change the DateTime properties in your Model to DateTime?.

Example:

// "Nullable DateTime", DateTime?, won't throw an error in the cast in EF
public DateTime? StartDate { get; set; }

And there you go!

Comments

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.