How can I deal with a CodeFluent Entities Business Layer and Database Layer concepts compatibility? - codefluent

I have a problem regarding concepts compatibility between business and data layer, with respect to entities / tables relations and foreign keys enforced on the database layer.
I am using CodeFluent MySql Producer, but I believe the behavior is the same on other Db producers as well.
The question involves one to many simple relationships between entities, which are mapped out to the db as INT datatype fields, with proper foreign keys enforced.
The issue I have is: on the business layer in .NET, the "linking" property for an entity is mapped as Integer type, with default value of -1 (minus one).
This means that, when the information for the linking of the two entities is NON-EXISTENT (not required), the business layer takes into account -1 as the content for the linking property, and this content -1 is also persisted to the database field on the correspondent table.
There lies the problem I need to understand. When I have -1 as content in the database, the associated foreign key is irrelevant / not compliant. This should be set to NULL on the database side, instead of -1. There is no -1 content as primary key in any row to the OTHER target table for the relationship.
One practical difficulty I have is: I can't user data synchronization scripts between databases that were populated with codefluent generated applications, because upon dropping the foreign keys at the beginning of a data synch script, I cannot re-create them later because of all the -1s (minus ones) in place of NULL that are present as a rule for non-existing information in fields representing relationship between 2 tables...
Can you help me understand and solve this issue?
Thanks

As you are using MySql, you have to add an attribute in the configuration file (app.config or web.config) to convert default value to NULL:
<configSections>
<section name="Sample" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
</configSections>
<Sample persistenceTypeName="MySQL"
connectionString="..."
mysql-useDefaultValue="true" />

Related

EF Core - why ClientSetNull is default OnDelete behavior for optional relations (rather than SetNull)

For optional relationships (when Foreign Key can accept Null), a new ClientSetNull behavior has been introduced since EF Core 2.0 as the default option for delete behavior DeleteBehavior.ClientSetNull.
This has SetNull semantics for tracked entities and Restrict (no action) behavior for database records not loaded into memory.
Microsoft docs say that:
If you want the database to also try to propagate null values to child
foreign keys even when the child entity is not loaded, then use
SetNull. However, note that the database must support this, and
configuring the database like this can result in other restrictions,
which in practice often makes this option impractical. This is why
SetNull is not the default.
But I think it is usually normal to set FK of dependent entities to Null when the associated parent is deleted (every where in db). And also, what's those "other restrictions, which in practice often makes this option impractical.." as claimed above?
Those other restrictions the docs are referring to are, as far as I know, circular or multi path cascades.
MS Sql Server for example does not allow cascades (both delete and set null) if
the change would cascade to the same table it originated from
there are multiple cascade paths to the same table. Like table 'A' affects table 'B' and 'C', both 'B' and 'C' affect 'D'.
You can't even create the constraint.
EF core can circumvent this limitation with ClientSetNull. EF handles the set null operation, but it can only do so if all the affected entities are loaded into memory.

Null values in relational database

I'm new in PostgreSQL(still learning)
I'm trying to create a relational database for a venue.
In my table(still in UNF) I have attribute to store the client's name, phone, email.
The problem is that the client will give maybe 2 or 1 info on him. So I will always have null values.
Sometimes I can get all the client's values(for the 3 attribute)
How am i supposed to deal with this in the normalization process?
Do I need to separate the tables in other relation. If so 3 relations is not too much?
For every attribute that should be there once, use a column in the main table. "Should" indicates it might be missing / unknown, too. That's a NULL value then. If the attribute must be there, define the column NOT NULL.
Attributes where there can be multiple distinct instances, especially if the maximum number is uncertain, create a separate table in a one-to-many relationship.
Store (non-trivial) attributes that can be used in many rows of the main table, in a separate table in a many-to-one relationship.
And attributes that can be linked multiple times on either side are best implemented in a many-to-many relationship.
Referential integrity is enforced with foreign key constraints.
It's not nearly as complex as reality, but the point is to establish a logically valid model that can keep up with reality.
Read basics about database normalization.
Detailed code example with explanation and links for n:m relationship:
How to implement a many-to-many relationship in PostgreSQL?

Create entities from database views using eclipse and JPA

Can we create entities classes from Database views using JPA Tools the way we do for database tables?
Manuj
You use the same syntax as you would for creating entity classes for a table. The only differences are: 1) typically views are read only, thus your entity will likely only be used as read only, and 2) Like all entities you need to have a primary key, thus your view will need to either have one column that has unique identity values, or a combination of columns that can be used as a unique identity.
The views are also available in the list of tables when you specify the connection and the schema.

jpa eclipselink modify field type

I'm making my first steps with JPA (eclipselink) developement, and i'm wondering if there's any good practice that may help reflecting changes brought to an entity into the data-base without re-creating the whole data-base.
In my case i have more than 30 entities, some of these are "correctly" defined and are already holding data rows on the data-base, but i've figured out that i made a mistake while defining some other entities by using the int type for the primary key instead of long, and the worst is that some of these entities are mapped by other entities, which means that they're used as foreign keys ... So i'm wondering if there's any "proper" way to deal with this without having to re-create the whole data-base. Re-creating the data-base will force me to re-fill the rows of the already filled tables and waist much time.
Thanks in Advance
George
ints and longs are mapped to a number column in database. Changing the type in the entities won't change anything. You might have to change the precision of the columns in database so that the number is sufficiently large to accept any long value, but doing that is trivial in SQL, using an alter statement.
JPA is just a way to access the database. The database is not supposed to be created by the JPA engine. You may do that, but you're not forced to do that. You may do what you want with your database using SQL, and do whatever you want with your Java code. If they both map together when running the application, it will work.
You can update your current schema automatically to reflect the modifications you've done inthe module by configuring EclipseLink like this:
<property name="eclipselink.ddl-generation" value="create-tables" />
For major modifications to your model it does not guarantee proper update to the schema, but for smaller ones it should work ok.

Is it possible to make an association between a Table and View in Entity Framework?

I have 2 databases (sql server 2005) in my system, one for configuration data and the other one for Application Data, but there are some tables that are needed in both databases. We've solved that using Synonyms but the problem is when we map the tables in Entity Framework.
We have a Language table in the config database, used for localization purposes. But in the application we have a table called "Countries", and it has a child table to contain the country's fields translated.
My Entity Framework Context maps tables in the Application database and the only way to map the Languages table from the other database is including a View created in the Application Database. Everything works fine, but when I try to make an Association between the CountryTranslation entity and the Language entity I get this exception:
Problem in Mapping Fragments starting at lines 733, 855: Non-Primary-Key column(s) [pai_codlan] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.
Do you know if this is a possible scenario? How do I resolve this?
Thx!
Solved!
All I had to do was delete the column that was mapped as a Entity Property and just keep the Navigational Property..