简体   繁体   中英

Exception using Linq query with EF Code first on SQL Server table

I have setup EF code first against my DB (specifically, a table called tblExpenseMain).

In my controller I'm trying to pass an instance of my 'ExpenseMain' class over to the view so the details can be presented. Manually creating an instance and passing it works:

ExpenseMain em = new ExpenseMain { Card_Number = "123", UniqueID_ERLineID = "blah", Item_Amount = 500, Item_Expense_Type = "something"};

            return View("_Basic");

But trying to query it with LINQ like so gives me an exception:

ExpenseMain model = (from e in db.ExpenseMains
                                 where e.UniqueID_ERLineID == transUniqueID
                                 select e).SingleOrDefault();

It tells me to check the innerexception to find the problem. I surrounded it with a try/catch and the innerexception message was:

e.InnerException.Message = "Invalid object name 'dbo.ExpenseMains'."

Any suggestions on what I'm doing wrong? I was hoping that the linq query would just grab my single instance and go from there. Changing 'Expensemain model' to 'var model' and looking at what the var becomes, confirms it becomes an 'Expensemain' anyway.

Thanks

Do you have several data access layers in your sulotion? Could it be that ExpenseMain is an old Entity, not generated from EF. Double check that there are no tblExpenseMain entity aswell or similar.

Hope this helps.

If your class name is different from the table name, then you have to configure EF to map them. You can use the table attribute and give the table name.

[Table("tblExpenseMain")]
public class ExpenseMain
{
    //properties
}

看来您这里有2个类别: ExpenseMainExpenseMains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM