I've got a problem with the entity framework as it's my first time using it along with SQL server. I managed to successfully create an entity data model with an entity called EMP. EMP has ID, Name, and Salary as scalar properties. I then generated the database from the model, copy/pasted the resulting sddl into SQL server and created my database. I went back into VS 2010 express and tried adding some records into the database using the following code:
string constr = ConfigurationManager.ConnectionStrings["dataemp"].ConnectionString;
dataemp db = new dataemp(constr);
db.AddToEmps(Emp.CreateEmp(0, "john", "Informatique", "10000000 cfa"));
db.AddToEmps(Emp.CreateEmp(1, "johny greg", "finances", "100000000 cfa"));
db.SaveChanges();//i get the error here
Console.WriteLine(
"*********Employee actuellement dans la database*********\n{0}",
query.ToString());
As a result the compiler gives me an exception as if I didn't connect to the database or as if it couldn't access the database but it is displayed in the database explorer. One more point in the database explorer: I can't see the tables(EMPs) whereas in SQL Server I am able to see it as dbo.Emps. Here is the exception the compiler shows me:
Unhandled Exception: System.Data.UpdateException: An error occurred while updating the entries. See the inner ex ception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'dbo.Emps'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStre am, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String res etOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Bo olean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Bo olean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnec tion connection, Dictionary2 identifierValues, List
1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapt er adapter) --- End of inner exception stack trace --- at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapt er adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at ConsoleApplication1.Program.Main(String[] args) in D:\Users\ITA Final\documents\visual studio 2010\Project s\zut\zut\Program.cs:line 13
I have read many tutorials on entity framework and linq to entities. I can't figure out what I am doing wrong here.
Invalid object name 'dbo.Emps'
Well, did you check to see if that exists? – tnw