Foreign Key Association and Independent Association, Whose priority is higher? - c#

If I have an entity in EF and I also have foreign key property in that entity. Now I want to update related record. If I use both Foreign Key Association and Independent Association, Then which will be preferred by Entity Framework and why ?
Case : Suppose If I assign different entity to navigation property and different enityId in foreign key property. Then what will be saved in database ?

I think the foreign key in your case. Independent key just for the table's itself

Related

How to setting a model with table don't have any primary key column

On setting DbContext, we have
modelBuilder.Entity<Person>(app =>
{
app.ToTable("Person");
});
The EFCore throws an exception:
The entity type "Person" requires a primary key to be defined
But our Person table doesn't have any primary key column.
How to avoid this ?
EF Core doesn't support tables without primary keys (aka heaps). The reason is simple: it needs to be able to manipulate individual records, something that cannot be safely achieved without a primary key.
As a resolution, you can add a dummy column/primary key of type int/identity or guid/uniqueidentifier, and just ignore it.

EF Foreign Key Associations Deleted

The picture show relation with two tables in my database;
Well as you can see there's a field called "DeviceTypeID" in the right side table.
Normally EF adds CompanyTypeID column to the ServiceLaburDefinitions model but it's disappeared last time I updated model from the database.
I am searching for a solution for a couple of hours but not able to find any solution. Could anyne suggest a solution?
Thanks.
ServiceLaburDefinitions is the depend entity and has the DeviceTypeID foreign key property defined.
So Entity Framework creates a navigation Key under the hood between the 2 tables based on DeviceTypeId key.
You can see for example how a navigation key is created also in the following example between the foreign key and the primary key
More information about navigation properties can be found here
When you create the model from the database, there is a checkbox marked "Include Foreign Key columns In The Model" - Make sure this is checked.

EntityFramework: 1:0..1 relation deserialized as collection

I have a problem with entityframewrok wich serializes a 1:0..1 relation between two tables as a collection.
I have 2 tables on my Database:
CREATE TABLE `revisiones` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
---Irrelevant columns here---
PRIMARY KEY (`Id`),
---Irrelevant constraint and foreign keys here---
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `ficha_deposito` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`IdRevision` int(11) NOT NULL,
---Irrelevant Columns Here---
PRIMARY KEY (`Id`),
UNIQUE KEY `IdRevision_UNIQUE` (`IdRevision`),
CONSTRAINT `fk_ficdep_rev` FOREIGN KEY (`IdRevision`) REFERENCES `revisiones` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE,
---Irrelevant constraints here---
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
As you can see a revision may be related as none or 1 ficha_deposito, due the unique restriction.
However on the edmx file, the relation is serialized as a collection:
If i try to change it manually (I would prefer to not do so, because if i have to regenerate the model, i will have to set the value again manually), then i get an exception:
Running transformation: Multiplicity is not valid in Role 'ficha_deposito' in relationship 'fk_ficdep_rev'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
Why can't I change the multiplicity of the relation? revision.ficha_deposito should be null or a simple object.
This is a EF6 limitation. EF naturally supports one-to-one relationship via so called Shared Primary Key Associations, where there is no separate FK in the dependent entity, but the PK is also used as FK to the principal table.
When the dependent entity is using a separate FK field, even if it's backed with an unique constraint, from EF perspective it's still one-to-many relationship. I can't explain it better than in the One-to-One Foreign Key Associations:
As you may have noticed, both associations in the fluent API code has been configured as a many-to-one — not one-to-one, as you might have expected. The reason is simple: Code First (and EF in general) does not natively support one-to-one foreign key associations. In fact, EF does not support any association scenario that involves unique constraints at all.
and then
The second limitation that this lack of support impose to us is more important: one to one foreign key associations cannot be bidirectional (e.g. we cannot define a property for the User on the Address class).
The good news are that such support has been added to EF Core, so when it becomes usable (v1.1 or later) you'll be able to set up such relationship. Until then, you should live with one-to-many.

Save entity in Entity Framework without a primary key

Scenario:
Database first.
I have a table with no primary key set and I'm trying to make an update with Entity Framework.
This is the error message I keep getting:
The property 'inactive_date' is part of the object's key information and cannot be modified.
If I set the fields 'Entity Key' value to 'false' I get this error messge:
Modifications to tables where a primary key column has property 'StoreGeneratedPattern' set to 'Computed' are not supported. Use 'Identity' pattern instead. Key column: 'timestamp'. Table: 'plat12Model.Store.glchart'.
Would this be corrected if I created a primary key? Can I set a primary key in my code rather than on the database?
By default, EF will make tables without primary keys and views into read-only classes where every field is part of the composite key. You can modify the conceptual model to reflect the actual behavior as long as you retain a key value that EF will use for object tracking. As the error message states, you also need to make the columns no longer computed in order to update them as well.

Add Navigation Property Manually to Entity Framework

I need to add a navigation property between two Entities TableA and TableB
TableA
ID : Primary Key
Code: String (Allows Null)
TableB
BID: Primary Key
Code: String (Allows Null)
Now I want to add a navigation property to these Entities which are related by the code which is not a foreign key. Can anyone tell me how this is possible
It is not possible because code is not PK in any of your tables. Navigation properties follows same rules as database relations - in principal table you must use PK and in dependent you specify FK. Databases also offers selecting unique key in principal table but EF doesn't support unique keys yet.

Categories

Resources