Click here to Skip to main content
12,944,738 members (52,161 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: , +
CREATE TABLE [dbo].[Deduction_1](
	[Id] [bigint] IDENTITY(1,1) NOT NULL,
	[Ded] [numeric](18, 0) NOT NULL,
 CONSTRAINT [PK_Deduction_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


DBContext
using System;
using System.Web;
using System.Data.Entity;
 
namespace ASPNETMVCApplication.Models
{
    public class BookDBContext:DbContext
    {
        
        public DbSet<book1> Deduction_1 { get; set; }
    }
}


Model Class
using System;
using System.Web;
 
namespace ASPNETMVCApplication.Models
{
    public class Book1
    {
        public Int64 Id { get; set; }
        public Decimal Ded { get; set; }
        
 
    }
}


Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPNETMVCApplication.Models;
 
namespace ASPNETMVCApplication.Controllers
{
    public class DeductionController : Controller
    {
        BookDBContext _db = new BookDBContext();
 
        //
        // GET: /Deduction/
 
        public ActionResult Index()
        {
            var deductions = from deduction in _db.Deduction_1
                             select deduction;
 
            return View(deductions.ToList());
        }
 
        public ActionResult Create()
        {
            return View();
        }
 
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
 
            Book1 deduction = new Book1();
            if (ModelState.IsValid)
            {
                deduction.Ded = Convert.ToDecimal(collection["Ded"].ToString());
 

                _db.Deduction_1.Add(deduction);
                _db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            else
            {
                return View(deduction);
            }
 
        }
    }
}


I got this Error
System.Data.SqlClient.SqlException: Invalid object name 'dbo.Book1'. Near the <pre lang="cs">_db.SaveChanges();
Posted 22-Nov-12 19:08pm
20119991.3K
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Please Put the attribute above the entity like this.
[Table("Tablename")]

Please make sure the tablename is the name of the table that we need to insert.
<pre lang="xml"><pre lang="c#">[Table("Tablename")]</pre>
<pre lang="sql">public class Book1
   {
       public Int64 Id { get; set; }
       public Decimal Ded { get; set; }
 

   }</pre>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,651
CHill60 3,055
Maciej Los 2,423
Jochen Arndt 1,910
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100