1
votes

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, List1 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.

2
Invalid object name 'dbo.Emps' Well, did you check to see if that exists?tnw
yeah i did it exist in sql server but the visual c# database explorer doesnt show it at all i dont know what to do ;the file is the same the connection string points toLilz Votca Love
Are you sure you are connecting to the right database? The message is crystal clear - Emps doesn't exist in the database you are connecting toPanagiotis Kanavos
well i thought so too but it displays the same database name which is employee.mdf into visual c# and when i click on test connection it worksLilz Votca Love
where will i find it the onmodelcreating?nevermind i found it on google ill go home and do as u saidLilz Votca Love

2 Answers

0
votes

The only way for this error to occur, is for the table to not exist in the database you're connecting to. The connection string you're using for the context cannot be correct. This could occur if the value of the connection string in the Web or App config files is wrong, or you're setting that connection string by hand and setting it wrong.

0
votes

Given that you are having a problem with something not existing in a database, which indicates bad connection, and given your connection string,

connectionString="metadata=res:///Model1.csdl|res:///Model1.ssdl|res://*/Model‌​1.msl;provider=System.Data.SqlClient;provider connection string='Data Source=.\SQLEXPRESS;AttachDbFilename="D:\Users\ITA Final\Documents\Visual Studio 2010\Projects\employee.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True'"

where the actual connection string used by ADO is

Data Source=.\SQLEXPRESS;
AttachDbFilename="D:\Users\ITAFinal\Documents\Visual Studio 
    2010\Projects\employee.mdf";
Integrated Security=True;
Connect Timeout=30;
User Instance=True;
MultipleActiveResultSets=True

I wonder why there's no Initial Catalog component to the connection string. You should determine the correct database within the MDF file to connect to, and include

Initial Catalog=MyDbName;

in the string. Immediately after the Integrated Security=True; piece will be a good place to put it.