Entity Framework SaveChanges doesn't work - c#

I'm trying to save some records into a SQL Server database using EF6 and this code:
_db.MyEntity.Add(item);
_db.SaveChanges();
I can see the code/stored procedure in SQL Server Profiler (RPC:Completed) so it's being executed and when I run the same SQL manually in Management Studio, then the insert works OK (item has been inserted without errors).
Is there a reason why this doesn't work?
Am I missing something (DbContext is OK as I have _db.Remove(item) few lines above that works fine)?

Ok, so the issue was caused by data type. SQL identity column was INT and in API model it was LONG.
As I said, I wasn't getting any errors on executions and the sql code copied from sql server profiler worked when executed.
Changing LONG to INT32 in API model resolved this issue for me (insert from API works).

Related

Why am I unable to delete records on local service based database with EF 6?

I have the following code to delete all records from a table. The table is in a local Service-based Database that will hold temporary pricing data on a desktop app. When this code runs, it logs that it connects, deletes, and commits the transaction. I can see that it has 1500 delete logs which matches the row count. But when I go to the table in Visual Studio, all the data is still there like nothing was deleted. If I run this code on my instance of full SQL server, I can delete rows just as expected. Does EF not work with local service based DB's? My work around is to use SQLCommand/SQLConnection etc, which works just fine. What am I missing here?
using (var context = new PriceListLocalEntities())
{
context.Database.Log = Console.Write;
context.PRICE_LIST.RemoveRange(context.PRICE_LIST.Where(x => x.PRICE_ID != null));
context.SaveChanges();
}
With a service based DB in Visual Studio, a copy of the .mdf file is copied to the output folder. An Entity Model connects to the output version, while raw ADO.NET connects to the project version. Using both types of ADO.NET in the same app with access and update them separately. Which doesn't work for operations that are dependent on each other.

Unable to execute Stored Procedure through SSMS and VS

I have a stored proc which just updates a table and I am executing it through C# code as well as through SSMS. Whenever i execute it, I get the following error:
Login failed for user domain\username
I have given full permissions to the SP and have also recreated the SP, but still I get the error. Strange thing is, all other procedures are working fine from both C# code and SSMS. I have also provided sysadmin access to my ID - still no luck.
While executing from VS (C# code), the connection opens but getting the same exception (SQLException).
Can anyone please tell me how to resolve this error?
Thanks Gregg for your suggestion. I have fixed this error. The problem was with the underlying tables the procedure was using and I have dropped and created those tables. Bingo.- it worked.
Looks strange but this is how I fixed.

Identifier must be declared ORA-06550 and PLS-00201

I have two databases; one is in local server;
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0 - Production
"CORE 11.1.0.7.0 Production"
TNS for 64-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
The other one is a VM :
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
"CORE 11.2.0.4.0 Production"
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
Everything else about these servers are identical.
I am DBA for both of the servers. All the tables and stored procedures and are working fine in both databases but one stored procedure is not working when Asp.net tries to connect to it.
When I run my Asp.net app that is connecting to the VM database I get the following error telling me that stored procedure must be declared :
Message: Sys.WebForms.PageRequestManagerServerErrorException: ORA-06550: line 1, column 7:
PLS-00201: identifier 'GETINFO' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I open my SQL Developer and debug the procedure on the VM database and I get the desired values.
It is only happening in .Net app if I try to get that particular stored procedure. Everything else, being all the other stored procedures, are working fine.
What I have done to fix the issue;
Changed the name of the procedure (didnt work)
Checked the schema to make sure all the tables and stored procedures belongs to correct user (they do belong to correct user)
I granted access to user even though stored procedure created by that user.
I dropped and recreated stored procedure
I used Myuser.storedprocedure name notation it didnt work
What I don't understand is that exact same code is working on local network, and also that I can debug the stored procedure via Oracle SQL Developer.
I can share the stored product here but it is really, really long.
How can I fix this?
You are getting this error because of insufficient permission to execute the stored procedure. Try to grant permissions at the user level (user level means through which credentials you are connecting to the database. Check in your config file).
I got the same issue. I created a stored procedure in Oracle DB. I executed, it compiled successfully. But, it was getting issue at runtime. After granting permissions at user level for the stored procedure then the issue resolved completely.
Run-time Exception ORA-06550: line 1, column 7:
PLS-00201: identifier 'Package.StoredProcedure' must be declared
ORA-06550: line 1, column 7: PL/SQL: Statement ignored
Check your connection string and make sure that you are calling correct database and/or schema name from C# application.
If all the other stored procedures are working fine and this specific one not working fine when you debug on oracle sql developer; it must be connection issue. If it debugs and work on both databases under DBA account there should be no permissions issues as well.
I think if you focus on connection as mmmmmpie said in his comment, you should find the issue.
I got this error when a stored procedure was changed in the database.
The script used for update the changes started with:
PROCEDURE myProcedure
By adding the following lines:
CREATE OR REPLACE myProcedure
Solved this error. In my case, maybe this error didn't show at first because it was a new stored procedure.
i got this issue when i was working with oracle and Asp.net MVC.
I was used store procedure.let me give you a in brief
we have two databases in oracle
eg. Reportdatabase, Productiondatabase
I was created store procedure on Reportdatabase and
also we have 2 connection in my web application eg: connection1, connection2
by mistakenly I was called connection2 Productiondatabase but query was
prepared on connection1 database so that some tables are not getting to Store procedure that's why I was getting this error

Stored Procedure doesn't exist, or does it?

I'm having a problem:
I have a db connection where I run stored procedures on. This same connection is used to create said stored procedures earlier on.
When I attempt to call a given stored procedure, later on, I get the following message:
Could not find stored procedure
'dbo.yaf_prov_upgrade'.
The problem is it actually does exist on the database. And there's also the fact that it shows up on the SQL Server Profiler.
RPC:Completed exec
[dbo].[yaf_prov_upgrade]
#PreviousVersion=46,#NewVersion=46 .Net
SqlClient Data
Provider Nico Matrix\Nico
I was wondering what could be the causes a particular query would throw such an exception even when it exists, it's called, and the call reaches the database.
It can't be a problem with the connection because it already executed other stored procedures. It can't be a problem with the procedure because it does exist, in fact the very same application, the very same web page, created it and put it there.
Update: forgot to mention I'm used integrated security, and I did run the SP on the database with the same user the application connects with, and I had no problem running it.
So what can it be?
Your RPC completed only means that the batch submitted to SQL Server was correct and completed. It doesn't mean the stored procedure ran and executed OK.
It will be (don't argue, check) one of:
wrong permissions
wrong database context
wrong server
stored proc is in a different database
To ensure that things are the same
SELECT
##SERVERNAME,
SUSER_SNAME(),
DB_NAME(),
USER_NAME(),
OBJECT_ID('dbo.yaf_prov_upgrade')
The OBJECT_ID will be NULL if the stored proc doesn't exist in that database or you don't have permissions.
I suspect it might be a permissions issue, check up if the user name your program is executing under has execute rights to the stored proc.
I'm no expert by far on ms-sql, but I do know it keeps SPs in a global cache. Is it possible the local connection only gets the global list of SPs upon connection? Maybe reinit the connection or re-select the cache?

Cannot create database file programmatically (SQL Server 2008 R2 Express)

I'm trying to create a database with LINQ to SQL programmatically.
I tried (following this):
MySQLSvrDb db = new MySQLSvrDb(#"c:\mydb.mdf");
if (!db.DatabaseExists())
{
db.CreateDatabase();
}
But I get a SQLException "A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
However, for unit tests I created a database the following way (making sure I have a empty db for every test):
MySQLSvrDb db = new MySQLSvrDb(#"C:\testdb.mdf");
if (db.DatabaseExists())
{
Console.WriteLine("Deleting old database...");
db.DeleteDatabase();
}
db.CreateDatabase();
This works fine. My problem is I don't see a difference to the first approach. The problem is maybe somehow related to this, but the suggested solutions didn't work.
Any hints?
EDIT
If I just skip the DatabaseExists() step it works, but I need to check, if there's already a Db.
How to: Dynamically Create a Database (LINQ to SQL)
How to create DB in Linq to sql
Did you try deleting the first created DB first? and run the method again?
Why you create Database locally,do you still attached it using sql server?
You can use sql lite or sql server ce DB instead.
Regards!

Categories