Foreign Keys generated as NOCHECK - codefluent

According this documentation page (Association Relationship), it seems that CodeFluent Entities generate Foreign keys in NOCHECK mode by default on One to Many and Many to Many relations. On the other hand, the Foreign keys on One to One relations are created in CHECK mode.
I have several questions about that:
My understanding is that NOCHECK foreign keys are disabled. If so,
what is the purpose of creating all that disabled foreign keys ?
Is there a way (and an interest) to change that behaviour?
We have some One to One relations on our application but still, all the foreign keys are disabled on our database. Why is it so ?
thanks by advance.

The foreign keys gives you some informations about the scheme even if there are not enforced. Also it can be convenient during development to not get integrity errors when you update the database. In the documentation, it is said that it's for performance reasons, but I'm really not sure about this one.
Anyway, you can instruct CodeFluent Entities to check constraint by setting defaultPersistenceEnforce="true" on the project
<cf:project defaultPersistenceEnforce="true">
From the documentation
Note: By default referential integrity is not enforced for performance reasons. However, one can enable relationships enforcement globally by setting the defaultPersistenceEnforce attribute of the project node to true.
It's also possible to enforce a specific relationship instead of the whole project. One can do so by specifying the persistenceEnforce attribute to true on the relation property.

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.

Entity Framework 6.x Must specify mapping for all key properties

I am beginning playing with EF 6.x and I've got an annoying issue.
I've designed a database with simple User,Role,Permissions tables each one bond to another with classic many-to-many relation.
EF wants me to create primary keys in indirect tables (UserPermissions etc.)
but also wants me to make some mapping for those Primary Keys but I have no clue why.
Is there something wrong with my design or there Is a workaround to this issue?
Maybe Can I in some way use Unique index instead of Primary Key to satisfy EF?
There you have diagram to make it more clear:
Well, I'm embarrased that I've not came up to solution earlier.
Just added multicolumn Primary Key on columns referencing direct tables via foreign key.
e.g. CONSTRAINT pk_IdRole_IdPermission_RolePermissions PRIMARY KEY (IdRole,IdPermission)

Entity Framework constraint naming in database

I have a problem with the naming of some primary keys in the database. On my development system the primary key constraints are named PK_FullTableName. Now on a production system the constraints are all named PK_PartofTableName_3214EC27164452B1 (with different Hex characters for each table).
The problem I have are the migrations. I have a code-based migration, that should change the primary key of a table and therefore has to drop the old constraint/primary key with DropPrimaryKey("FullTableName", new[] { "Field1", "Field2" });
On my development system everything works fine, on the productive system I get an SQL exception that the constraint PK_FullTableName could not be found and I cannot migrate.
I don´t know why Entity Framework names the constraints different on these two systems. Does anyone know what this behavior is depending on? Is this naming from the EF or from the database(MSSQLExpress)? And is there a workaround?

Composite DB keys with Entity Framework 4.0

The re-design for a large database at our company makes extensive use of composite primary keys on the database.
Forgetting performance impacts, will this cause any difficulties when working with this db in Entity Framework 4.0? The database structure is unlikely to change and I'm not looking for "philosophical" debate but what are the practical impacts?
According to Jeremy Miller, "Composite key make any kind of Object/Relational mapping and persistance in general harder." but he doesn't really say why. Is this relavent to how Entity Framework 4.0 handles keys?
No, EF4 supports composite keys just fine.
The problem is a table with a surrogate key and composite keys. You can only set a single key on each model; that key can have multiple fields, but you can only have one from the designer standpoint. Not sure about manually editing xml or code only mapping.
You can set a field as an Identity and not a key if you need a composite and surrogate key on the same table. The Identity ( Id ) field won't be used by the ObjectContext or ObjectStateTracker but will increment and be queryable just fine though.
I have had problems with EF4 and composite keys. It doesn't support columns being used as components in more than one key in a join table.
See my previous question Mapping composite foreign keys in a many-many relationship in Entity Framework for more details. The nuts of it is that when you have a join table (describing a many-many relationship) where both of the relationships use a common key, you'll get an error like
Error 3021: Problem in mapping
fragments...: Each of the following
columns in table PageView is mapped to
multiple conceptual side properties:
PageView.Version is mapped to
(PageView_Association.View.Version,
PageView_Association.Page.Version)
The only way around it was to duplicate the column which defeats the purpose of having it there at all.
Good luck!

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven't found a way to declare unique constraints or check constraints.
Is this feature missing on the designer or on the framework?
Support for unique keys/constraints does not exist in ADO.NET Entities in v4.0, see the answer to "one-to-one association on a foreign key with unique constraint", where Diego B Vega says:
I know for sure we haven't added
support for unique keys other than
primary keys in 4.0.
He does, however, provide a possible workaround/hack (which comes with all the normal caveats):
As you are probably aware of, it is
often possible to “lie” to Entity
Framework and tell it in the SSDL, for
instance, that some unique key is the
primary key. I reckon this would work
very well if the actual primary key is
an surrogate key (i.e. an IDENTITY
column that was added for this
purpose) and you don’t even have to
map it in the model.