V
V
V Sh.2015-09-03 10:20:35
C++ / C#
V Sh., 2015-09-03 10:20:35

How to remove element from DbContext Entity Framework?

Good afternoon!
Please advise how to fix the following situation. There is a BookContext db (DbContext), which is already connected to the table of the same name in the Oracle database. New entries from the code are added perfectly, i.e. line

db.Books.Add(new Book { Name = "First step", Author = "shavadre", Price = 120 })

works great. Problems begin when I try to delete record from a context and, respectively, from a DB.
Book delBook = db.Books.Where(x => x.Id == 2).FirstOrDefault();
            db.Books.Remove(delBook);
            db.SaveChanges();

An error pops up:
The specified value is not an instance of type 'Edm.Decimal'
Parameter name: value

Class and context code:
[Table("BOOKS",Schema="VRSHAIKHISLAMOV")]
    public class Book
    {
        [Column("ID", TypeName="NUMBER")]
        [Required]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        
        [Column("NAME", TypeName="VARCHAR2")]
        public string Name { get; set; }

        [Column("AUTHOR", TypeName = "VARCHAR2")]
        public string Author { get; set; }

        [Column("PRICE", TypeName = "NUMBER")]
        public int Price { get; set; }
    }

public class BookContext : DbContext
    {
        public DbSet<Book> Books { get; set; }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2015-09-03
@JuniorNoobie

Try 2M instead of 2

I
Ivan Filatov, 2015-09-03
@NYMEZIDE

Book delBook = db.Books.Where(x => x.Id == 2).FirstOrDefault();

Find Why don't you use Id for the key?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question