Configuring a centralized entity - ef-core-3.0

I have a very centralized table "RecordInfo" which holds some general data
e.g.: UserModified, UserCreated, DateTimeCreated, etc.
This table should be accessible via a navigation property from any other table in my application. Therefore I'm using a GUID Column to join the tables.
So it acts similar an one-to-one relation, but I cannot make a Foreign Key to multiple target tables. This will not work due to the FK constraints.
So I'm searching for a solution to implement navigation properties, without defining a ForeignKey - is this possible in any way?

Related

How to create a foreign key with NOT NULL in Keystonejs 6?

I wish to know how to create a foreign key with NOT NULL in Keystonejs 6 Schema file.
I use postgresQl AND ORM Prisma.
I can't create a relationship field with isRequired = true, which means NOT NULL.
Someone can explain how to add NOT NULL for the relationship field in Keystonejs 6 Schema file? Either maybe it's impossible?
Yeah, relationship fields currently don't support the validation.isRequired or db.isNullable options. This is true even when the list being configured holds the foreign key (ie. a many-to-one relationship or one-to-one with db.foreignKey: true).
There are plans to support these options but the work isn't trivial. For example, these constraints can effect the order in which nested creates need to be performed. Keystone will also need to validate the config makes sense and doesn't for example, have isNullable: false on both sides of a one-to-one relationship (which would make inserting records impossible).
If you want to emulate similar functionality right now it's possible using hooks. I think you'd need...
A validate-input hook on the list with the foreign key, to ensure a item was linked on create (and not removed on update)
validate-input and validate-delete hooks on the other list to ensure links weren't broken when updating or deleting items from the other side.
Since this solution's implemented in the app layer it doesn't give you as strong a guarantee as a proper database constraint, but it's a start.

Core Data: Cascading 1:1 Relationships

Scenario: Cascading 1:1 Relationship as shown below.
I merely want to have an inverse relationship amongst a few entities, based on the userID field.
Questions:
1) How does the relationships know which field to link to?
All I did was indicate target and give the relationship a distinct name.
If the relationship is independent on the supplied userID, then I assume such field is superfluous; correct?
2) You'll notice that I need two (2) relationships: userID & userID2.
Essentially I merely want to link all common userId values across files. Is this the correct setup?
Core Data isn't a relational database, therefore you don't need a specific field to create a relationship between objects.
Typically you use relationships to model what would be a property on an object.
If you want UserConfigurations, UserCredentials, etc. to be related to a User then you should create a relationship between User and each of the user specific objects.
Then you can access a users configuration somewhat like this:
user.configurations
If you have set up inverse relationships, which Core Data recommends, you can also access the User from the UserConfigurations object. That allows you to access other parts of the object graph easily, e.g. you could access a users events from a users configuration:
configuration.user.events
This way you don't need relationships between the different objects that are related to a user.
Your data model should look similar to this:

How to properly preserve relations between content items on export/import in Orchard?

I've read the official documentation on creating 1:N and M:N relations and there's one particular aspect that isn't covered: support for importing and exporting the relations. Since the relation is defined implicitly using the primary keys (auto-incrementing integers), won't that be a problem when exporting the data for import in another environment (like in a backup/restore scenario)? For instance, the order of items should matter during the import. Also, the internal id values won't necessarily be the same after an import to a fresh Orchard installation (since they are auto-incrementing).
What is the preferred way of implementing relations that support importing and exporting?
This is solved by using the identity feature that is provided as part of the import/export api. Instead of referring to a primary key value that is pretty much guaranteed not to be valid on the target instance, it's generating a deterministic and unique id that enables proper transfer of items, including in cases of relationships. There are two identity providers out of the box. One uses the alias of the item (when that exists) and the other stores GUID (that's the identity part, used by widgets for example).

Domain Driven Design and local identity in an aggregate

In Domain Driven Design there is an Aggregate Root that has reference to internal entities.
Aggregate Root is an entity with global identity (everyone able to use its id). Aggregate root has links to local objects (entities).
Assuming here that Entities are the Hibernate #Entities (let's say)
Let's say we have Aggregate Root "User" that has "Address" entity in it as an object (which is actually an entity as well)
The question is:
How is it possible to make local entities to be with local identity only. I mean, there is no any barriers that could prevent anyone to use local entities (like Address) by its IDs. (so then this identity is not local at all, but global). Then, what is the way to make it local?
Well i don't think this is a matter of a public field or property or some access restriction mechanism, the way i see it "local identity" means that objects outside of the aggregate boundary can't use that local identity in a meaningful or useful way (e.g. they can't use that identity to retrieve that object or persist it to the database or any other operation). That identity doesn't mean anything to the outside world and it is only unique within that aggregate. Another example, what guarantees you that objects outside of an aggregate boundary won't hold references to objects within (which violates one of the principles of aggregates), well nothing unless those objects are VALUE OBJECTS which might not be the case every time. If i want to put that in a few words: Don't create any public APIs that use identities of objects within an aggregate , this way you will make it clear to the developer not to use those IDs.
All entities, including the root, have an identity. The fact that only the identity of the aggregate root should be used "globally" is something that cannot be easily enforced by the code itself. In a relational database in particular, every table record will have some key, regardless of whether that record stores an aggregate root, and entity or a value object. As such, it is up to the developer to discern which database identities are part of the domain and which are not.
Entities within an aggregate root are supposed to only have local identity. For all intents and purposes the database table need not have a primary key. When the aggregate is hydrated the entities within the AR should be fetched based on their link to the AR. But even that FK need not be represented in the local entity since the connection is obvious based on the containment of the local entities with the AR.
Since most database systems will moan if there is no PK on a table so you could add one for the sake thereof but you can just ignore it in your entity design. So there would be no property for the PK in the entity. The only way someone could then get to that entity is by way of the DB since there should be no way in your code to do so.

Creating a glue table with Core Data?

Let's say I had one entity called "Garage" and one entity called "Cars".
In SQL I might create a third table (sometimes called a glue table) that would just have two fields "GarageID" "CarID"
That way I could relate certain cars to certain garages.
How would this be handled with the Core Data modeller?
Thanks!
You are talking about many-to-many relations. CoreData supports such model. Just create in both entities relations to each other set as "to-many". That's all.

Resources