EF Core 6 HasDefaultValueSql with multiple sequences - sql-server

I'm currently re-writing an old service, and ran into an issue.
In the SQL Server we have a sequence for each clients product number. The old version used stored procedures for creating products. But I was looking into if it was possible to do something with HasDefaultValueSql.
The MS docs said to use .HasDefaultValueSql("NEXT VALUE FOR sequence"), but is there some way to differentiate which sequence to use based on the client_id column?
My product entity type configuration:
builder.ToTable("products");
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).HasColumnName("product_id")
.ValueGeneratedOnAdd();
builder.Property(e => e.ClientId).HasColumnName("client_id");
builder.Property(e => e.Number).HasColumnName("number")
.HasDefaultValueSql("NEXT VALUE FOR client_idProductNumberSequence");
builder.Property(e => e.Name).HasColumnName("name");
builder.Property(e => e.Description).HasColumnName("description");
...other props
builder.HasIndex(e => new { e.ClientId, e.Number })
.IsUnique(true);
If thats not possible, what would be the best way to solve this?

Related

How to get the current Mongoid session or database

I have a multitenant app and I use Mongoid.override_session(current_user.customer_id) to point to the appropriate customer database. I'm using paperclip to save photos to an S3 bucket and I'd like the folder structure to start with the customer_id but I can't find a way to get the current session or database from Mongoid. I found this question but the answers are not working for me on Mongoid v4.0.1. current_user is out of scope in the model code that sets up the URL for paperclip as is the paperclip initializer.
In case it helps someone else, here is a method I found that you can run on the class or an instance of your model
mongo_session.options[:database]
I added the following to my paperclip initializer;
#myapp/config/initializer/paperclip.rb
Paperclip.interpolates :customer_id do |attachment, style|
attachment.instance.mongo_session.options[:database]
end
Then you can reference :customer_id in your model
has_mongoid_attached_file :image,
:storage => :s3,
:s3_credentials => File.join(Rails.root, 'config', 'aws.yml'),
:styles => {
:medium => '600x600>',
:small => '300x300>',
:thumb => '100x100>'
},
:path => ':customer_id/:style/:filename',
:url => '/:customer_id/:style/:filename'

Error occurred while writing a data into CouchDB

I am trying to Delete the whole data from the CouchDB and again i am trying to write same data with modified **_id field and some extra field **
but i am getting following error :
{
'reason' => 'Document update conflict.',
'error' => 'conflict',
'id' => 'test_1'
},
{
'reason' => 'Document update conflict.',
'error' => 'conflict',
'id' => 'test_2'
},
How to resolve the error ?
When creating new document "test_1", there should be a document with that name already having a different _rev in your db.
If you need to update the old "test_1", you need to provide the _rev of that document when updating. Or else, you can delete "test_1" and then try creating another document with the name "test_1".
The point here is, you should provide the latest _rev of a certain document, when updating that document.

How do I build this complex database query in CakePHP 3.0?

This is my database design, or at least the tables that are relevant to this question.
I want to build a query that returns a single page (find will be based on the path attribute), with the associated container, with its associated child-containers in threaded form and all of those containers should individually have their associated blocks with them (preferably in the right order sorted by the index column from the blocks_pages table).
Can anybody give me a clue how to wrap that all up with the query-builder? Or if that is not possible, then is it possible to do it using the new map/reduce feature, since the after-find function has been removed?
In case it helps, this will be the visualized result, if you just ignore the magenta Article-box for a moment.
Try this
$pagesTable
->find()
->where(['path' => $myPath])
->contain([
'Containers.ChildContainers' => function($q) {
return $q->formatResults(function($results) {
return $results->map(function($container) {
$container->nested = $container->source()
->find('children', ['for' => $container->id])
->find('threaded')
->contain(['Blocks']);
return $container;
});
});
},
'Containers.ChildContainers.Blocks'
])

CakePHP virtualField find all not null

I have a database table "transactions" which has a field "account". I want to retrieve a subset of all not-null account rows from the current set of data and have it as a virtualField I can access down the line in my view.
class Transaction extends AppModel {
public $virtualFields = array(
"Accounts" => $this->Transaction->find("all", array("conditions" => array("not" => array("Transaction.account" => null))))
);
}
So that I get an array of all transactions with non-null account fields named "Accounts".
This doesn't work - gives me "unexpected T_VARIABLE" error (doesn't like $this). I was trying to follow the guide here. I'm a moderate level PHP developer and this is my first real Cake project, so I may be going about this completely wrong.
When you're inside the model that you're querying, you don't specify the model name, just:
$this->find('all'); // when you're inside transaction model
...so try this:
"Accounts" => $this->find("all", array("conditions" => array("not" => array("Transaction.account" => null))))

Building Many-to-many query with EF

I'm using EF5 with .net 4.0 and I have 3 entities - User, Project and Interest. I connected User Many2Many Interest and Project Many2Many Interest using FluentAPI (it created additional 2 tables for each relation). Everything works great.
What I want to do is to load all projects per user who has same interests. I tried
Project ...
.Where(p => p.Interests
.Any(t => user.Interests.All(i => i.Url == t.Url)));
(Interest has PK Url). When I perform this query, I get error
Unable to create a constant value of type 'DAL.Models.Interest'. Only primitive types or enumeration types are supported in this context.
What is the right query?
The problem is you can't translate user.Interests.All(...) into a SQL procedure.
You can however, create a list of the primitive type (Url) and then compare against that.
var userInterests = user.Interests.Select(u => u.Url);
var sharedProjects = m.Projects.Where( p => p.Interests.Select(i => i.Url)
.Any(pi => userInterests.Any(ui => ui == pi)));

Resources