Creating database triggers via Fluent API - sqlite

I am using ef-core 2.2 in connection with sqlite. In order to prevent/catch concurrency exceptions, I am creating a concurrency token in the according table. The token is a timestamp which I am configuring in my DbContext class via Fluent API:
modelBuilder.Entity<User>().Property( u => u.Timestamp ).IsRowVersion();
After that I need the timestamp to be updated on every update to my table-entry, therefor I am appending a sql statement to my automatically created migration file:
In Up( MigrationBuilder migrationBuilder ):
migrationBuilder.Sql( "CREATE TRIGGER SetUserTimestamp AFTER UPDATE ON Users BEGIN UPDATE Users SET Timestamp = randomblob( 8 ) WHERE Id = NEW.Id; END" );
In Down( MigrationBuilder migrationBuilder ) I append:
migrationBuilder.Sql( "DROP TRIGGER SetUserTimestamp" );
This solution does work well. One problem for me is that I have to be extremely careful not to forget including the trigger creation after I created a new migration via command line. Is there any way to configure this trigger in the DbContext class, maybe via Fluent API? Or is there another way to automate the creation of the trigger?

Related

Entity Framework Core Scaffold - Dynamic Schema Name

I'm having an issue at the moment where, we have a database which was already created, so used ef scaffold to create a model of it - the schema the model was created against is called "xxxx-dev".
Now, this has been fine, but in preparation to go live, I created a new DB server and provisioned the database to be called "xxxx-live". Switched the connection string, and attempted a query against it, and got an error.
It seems that scaffolding has hard-coded the schema name into every table in the OnModelCreating call, for example:
modelBuilder.Entity<xxxx>(entity =>
{
...
entity.ToTable("xxxx", "xxxx-dev");
...
});
This is a bit of an issue as going forward, we might have multi-tenant sites based on the same database, and obviously the query overriding the connection string every query isn't a great experience.
Is there anyway to configure the schema name, either in the Scaffold, or at runtime? I've done a bit of searching around and can't seem to see a solution.
Thanks,

How to delete all data in a partition?

I have a CosmosDB collection with a number of different partitions. I want to delete all of the data in one of the partitions so I tried to run the command:
db.myCollection.deleteAll({PartitionKey: 'pop-9q'})
Where PartitionKey is the field that I partition/shard based on. But when I execute this it returns the not very helpful message:
ERROR: An Error has occurred
Why would I be getting this message and how can I either get more details on the cause or find a resolution?
Currently, at this time, you are unable to perform a bulk delete. Please Up Vote and Comment on this functionality: Add the ability to delete ALL data in a partition
Additionally, which API are you consuming? For Gremlin API you could execute something like the following: g.V().drop()
The Microsoft.Azure.Cosmos SDK has added this ability - currently only available as a preview feature (which requires you to opt-in via the portal)
See here for more details:
https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-delete-by-partition-key?tabs=dotnet-example
Sample code included there:
// Get reference to the container
var container = cosmosClient.GetContainer("DatabaseName", "ContainerName");
// Delete by logical partition key
ResponseMessage deleteResponse = await container.DeleteAllItemsByPartitionKeyStreamAsync(new PartitionKey("Contoso"));
if (deleteResponse.IsSuccessStatusCode) {
Console.WriteLine($"Delete all documents with partition key operation has successfully started");
}
As #Mike said, a "delete all data" feature is not supported yet in Cosmos db SQL API and Mongo API. I notice that you have already added comments in above link. I just provide you with a workaround here that using bulk delete stored procedure for Cosmos db SQL API.
(sample code: https://gist.github.com/deepumi/2a23c5380202bddf0b85e83baf5833be)
For Mongo API, unfortunately, even stored procedure is not supported. You could create an Azure HTTP Trigger Function to execute bulk delete code in the function whenever you want or merge it into your program code.

Symfony - Log runnables natives queries when database is out

I'am working on a Symfony app that provides a rest web service (simple HTTP Request with JSON).
That service check some rules and inserts few lines in two MySQL table (write only).
For optimize reason, even if Doctrine bundle is available, i use native MySQL Query (with bind params) to insert this lines.
My need is : If for any reason, the database is not available, write "runnables" queries into a log file.
The final purpose is that when database is back, i want to be able to execute directly the file's content on the database.
Note that there is no unique constraint (pk is a generated uuid) and no lock or transaction to handle (simple insert statements).
I write a custom SQLLogger, but when $connection->insert(...) is called, the connect fail before logger is called.
So, my question is : There is a way to get the final query (with binded parameters) without database connection ?
Or should i rewrite the mecanism that bind params into query and log it myself when database is not available ?
Best regards,
Julien
As the final query with parameters is build by the database, there is just no way to build the query with PHP and to be garanteed that the query will be the same as the database.
The only way si to build query without binded parameters, but this is clearly not a good practice.
So, i finally decided to store all the JSON (API request body) in a file if the database is not available.
So when the database is back, instead of replay SQL queries, i can replay the original HTTP query.
Hope this late self-anwser will help someone.
Best regards.

alfresco-access application query filtering not working on 4.0.e

I have created application which is accessing alfresco using web service client. Now I want to audit all events of that application in alfresco, so I have enabled alfresco-access audit application. Here when I execute simple audit query it is returning all results properly, but when I execute query to get all audit events related to specific file it returning 0 entries.
Simple Query to get all audit events
http://localhost:8080/alfresco/service/api/audit/query/alfresco-access?verbose=true
Advanced Query to only get audit events of specific file
http://localhost:8080/alfresco/service/api/audit/query/alfresco-access/transaction/path?verbose=true&value="/app:company_home/cm:test/cm:test1/cm:test2/cm:testfile.pdf"
I have added following configuration in my alfresco-global.properties:
audit.enabled=true
audit.tagging.enabled=true
audit.alfresco-access.enabled=true
audit.alfresco-access.sub-events.enabled=true
audit.cmischangelog.enabled=true
audit.dod5015.enabled=true
audit.config.strict=false
audit.filter.alfresco-access.default.enabled=true
audit.filter.alfresco-access.transaction.user=~System;~null;.*
audit.filter.alfresco-access.transaction.type=cm:folder;cm:content;st:site<br>
audit.filter.alfresco-access.transaction.path=~/sys:archivedItem;~/ver:;.*
Following query is now working, just need to add application name in key parameter -
http://localhost:8080/alfresco/service/api/audit/query/alfresco-access/alfresco-access/transaction/path? verbose=true&value=/app:company_home/cm:test/cm:test1/cm:test2/cm:testfile.pdf

Retrieving Data from the Database ASP.NET MVC + Oracle

I have two tables
Users (Userid, Name, PhoneNumber)
Applications (ApplicationsId,UserId, ApplicationName, ActiveDate)
Every user will have more than 1 application.
In Nhibernate using lazy loading I can get the users data along with all the applications for every user. So, I used to do something like user.applications[i].Applicationname to get all the applications.
But, Now how do i retrieve all the applications along with the users data using oracle commands. I know how to get one application using joins. But, how do i retrieve multiple applications and store it in a IList. Any help is greatly appreciated. Thank you.
First you should download the Oracle Data Provider for .NET in
http://www.oracle.com/technology/tech/windows/odpnet/index.html
after you make sure that you can open a connection to your oracle database then
define mapping file of your entities in Nhibernate and map table columns of your oracle table to .NET object. after that you can use the following code snippet to get the list of your entity from database
// create the query...
IQuery query = session.CreateQuery( "from Post p where p.Blog.Author = :author" );
// set the parameters...
query.SetString("author", (String) instance);
// fetch the results...
IList results = query.List();
You should define the (Post) entity and (Blog) entity to your mapping file and as you can see (Post) has relation to (Blog) entity which is defined in the mapping file too.

Resources