I'm using EF Core and I'm trying to use FromSqlRaw to delete all records from a table. While it doesn't throw an exception it also has no effect on the table.
I have:
db.MyTable.FromSqlRaw("DELETE FROM dbo.MyTable");
What am I missing? What I want to avoid is having to fetch all the data from the table before deleting it.
The output log just says:
The thread 0x3a44 has exited with code 0 (0x0).
Microsoft.EntityFrameworkCore.Infrastructure: Information: Entity Framework Core 5.0.1 initialized 'MyContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
FromSqlRaw is for queries that return entities. You instead want ExecuteSqlRaw, which is for queries that do not return entities and can be found in the NuGet package Microsoft.EntityFrameworkCore.Relational.
Related
I'm new to Ef core. Using migrations. I made some changes in my code recently.
Got following error:
The operation failed because an index or statistics with name 'IX_JobPosts_Role_Domain1_Filters_Item1_ExperienceLevelId' already exists on table 'JobPosts'.
Ok, so I did what I usually do when I encounter errors, delete my database and previous migrations and start all over again.
First migration works fine everytime. Second migration prompts the same error even though no changes has been made to my code.
Why does it keep adding columns that already exists?
the reason is that ef core does not support nested owned types.
I'm using an existing database and I have mapped one of the tables as an entity (as i needed to map a foreign key).
So when it comes to initialising this database I would like EF to ignore this entity since it already exists.
How would I go about doing this?
You should create an empty migration as the first migration with the -IgnoreChanges flag to allow Entity Framework to get a snapshot of the existing database.
So out of the gate use:
Add-Migration InitialMigration -IgnoreChanges
and that will create a blank migration but it will update the Entity Framework metadata allowing the existing tables to exist and not be touched by migrations.
Also to be mentioned that the naming conventions that Entity Framework expects and your database schema may differ. You may need to manually setup the foreign keys using the Fluent API.
I didnt check with EF 6 specifically, but I think default EF behavious is that when the database exists, then it presume all model be ready and therefore will create no tables. If you want your initialization code to create tables with code first, use initialization code for prepare data. Look here :
http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx
I have DocumentItem entity mapped to insert/update/delete stored procedures in Entity Framework edmx.
I'm trying to insert a new Document into the databse along with its DocumentItems. The whole operation is enclosed in a transaction, and it's not easy to debug separately.
This is why I would like to try to debug the sp 'live' - when it's called from entity framework. Is it possible at all?
Just use profiler to see what data EF sends to stored procedure and use that data separately to test / debug only stored procedure. Debugging it together requires you to set debugging session for both .NET code and SQL code and place breakpoint into stored procedure prior to calling SaveChanges on your context. In theory it could work but I never use that.
I am trying to call a stored procedure using native query call from an entity manager like this
String command = "..."//my stored procedure call command
Query q = getEntityManager().createNativeQuery(command);
But when i run it I got the following message:
Exception, procedure ... can be run
only in unchained transaction mode...
I know if I have a connection object, i can set con.setAutoComit(true) to make this work.
But my question is: since I have a entity manager object, can i set this somehow from entity manager object? I'd like to have the container to manage all the database resources...
I am using EclipseLink.
thanks.
One more note: i don't have the control over the database side,so i cannot go and change the transaction mode to "any".
I found the solution:
getEntityManager().createNativeQuery("set chained off").executeUpdate();
What database/JDBC driver are you using?
Are you using Sybase JConnect? I think it had this issue with some stored procedure calls.
If you cannot fix the issue on the database/drvier, EclipseLink has an option for this.
Using a SessionCustomizer you can set,
session.getLogin().handleTransactionsManuallyForSybaseJConnect();
This will only work if EclipseLink has control of the transactions, not with JTA.
I have to delete all rows of a model with condition fkey_id = 1
What 's the better mode?
Agree completely with Devart, for batch Deletes/Updates use standard SQL.
If you are using .NET 4.0 the ObjectContext has some new methods for calling directly.
In particular:
ObjectContext.ExecuteStoreCommand(string commandText, params object[] parameters)
The fast way is to use batch commands in pure ADO.NET or a stored procedure.
Entity Framework does not support batch execution and deleting the records not attached to context.
The common way to delete using Entity Framework is to explicitly load the collection of objects associated with the fkey_id == 1 and call the delete method.