Foreign key contraints interpretation problem - c#

I generate Entity Data Model in Visual studio from the database.
However, I noticed that it does not generate neither relationships nor navigation properties from some foreign keys. It occurs when the foreign key contsraints are defined with the disabled option Enforce Foreign Key Constraint, like in the following exhibit (from SSMS).
Is there any way to deal with this? Unfortunately I cannot alter my database schema.

You can add them manually from the designer but it can have some consequences depending on the version of entity framework yo are using.
If you simply add association from the toolbox it by default creates independent association. Independent association must be mapped to the database counterpart. It means that you must manually open EDMX and cheat EF by modifying SSDL (you will add relation to SSDL part manually which can be quite hard task - follow SSDL reference: AssociationSet and Association elements). Now you can map the relation in the Mapping details window of the designer (you will also have to modify entities because FK property mustn't be mapped to the entity when independent association is used and in case of many-to-many association you will have to remove entity for junction table). EF will believe that this relation exists in the database. Once you modify SSDL manually you cannot use Update from the database any more. It will delete all your manual changes.
If you are using EFv4 you can use foreign key association (complete differences between those two types is described here). Foreign key association is not mapped but it cannot be used to define many-to-many relation.
The problem will occur if data in the database doesn't enforce the relation (which can happen because FKs are disabled). In such case your application will not work and there will be no way around this except repairing data integrity in the database or removing the association from the model.
The best solution for you is turning on FKs in the database!

Unfortunately You have to add those by hand in the model. That's the power of OR Mapping. Model can look different (better) than database.

Related

How do you manage duplicate relationship in Entity Framework Core when working on Dynamics CRM?

I am working with Dynamics CRM and wanted to test the efficiency of entity framework core to generate the models and context for it's SQL Server database.
Using Scaffold-DbContext, I ran into this error :
The foreign key {'OwningBusinessUnit'} cannot be added to the entity type 'ActivityPointerBase' because a foreign key on the same properties already exists on entity type 'ActivityPointerBase' and also targets the key {'BusinessUnitId'} on 'BusinessUnitBase'
Quick look through the DB and found a relationship duplicated. Meaning: both foreign key constraints target the exact same foreign and primary key field.
They are perfectly identical, only the name differs.
Surely enough the Microsoft documentation shows that to be the case and I found both of these relationship mentioned.
business_unit_socialactivity
business_unit_activitypointer
I also found other such case.
Can I delete any of the two foreign key constraint (doubt it) ? If not, how do I manage this issue ?
I am using Entity Framework Core and .NET Core 3.1.25.
Is this a version issue?
If by "delete" you mean, delete the columns in the database, I would strongly advise you not do that. Typically you don't want to change the models of a system you are using unless you own the system and have a strong understanding of why it was there in the first place. If by "delete" you mean, not include one of the properties in your entity framework model, that will work until you need to update the table, which won't work at that point because the model won't know to update the missing column and may cause the data in the schema to become not well formed (e.g, missing an update to the table's column that should have happened).
Entity Framework supports having two foreign keys to the same table. You may need to set up your foreign key relationship differently. I'm guessing that the entity framework model might be reusing the same property for both foreign key relationships. If that's the case, then you need to add a second property representing the second foreign key.
It seems suspicious to me that the schema has a duplicate foreign key for the exact same purpose. I'd double check to make sure your understanding of its purpose is correct. It could be due to a refactor that Microsoft did to get a new standard name while retaining backwards compatibility in parts of their system or customer systems. If both foreign keys actually represent the same relationship (I think this is unlikely) Consider instead not specifying that both are a foreign key in the entity framework model. Specify that one is the foreign key, and don't specify that the other one is. Entity Framework will treat that as the property for navigation and join purposes, and think of the other as just a data column. Then it'll be up to you to make sure the other column is set correctly if it's changed.
If the entity framework model and configuration is being generated from a tool and you're getting this error, you may have to specify the model and configuration by hand to get the correct behavior.

Using EntityFramework C# code-first from database - how to map table with no primary key

I am building an Ntier application with EntityFramework c#.
I am adding an Entity Data Model in my Data Access Layer with code-first approach from existing database.
Some of the tables of my db weren't included because they don't have primary key. I have seen some ways to work around this problem, modifying EntityFramework's edmx to force the mapping to the database, disguising some field like a key. But I am not using the .edmx, since I can't use automatic migrations with it. I only generate POCOs from my existing database and then go on with code first migrations.
Is there a way to force Entity Framework to generate a POCO for those tables without primary key ? Some only have one entry and really don't need PrimaryKey
In the end, I just wrote my own POCOs for the tables that weren't included.
I used an attribute [KEY] above the property i wanted to act like key. I added DbSet lines in the DataModel and EF did recognize them in my database.
I didn't want to generate primary keys because my boss didn't want, and thats a reason good enough. :) Hope the best for you thx for answer

Entity Framework: Is it possible to model an association without a corresponding foreign key

We have a database under complete control of dba's who refuse to add any foreign keys to the schema (don't ask).
I did not create the database schema, nor can I change it.
So far, we haven't found a way to model associations between entities, because EF refuses to do so without a foreign key in the schema.
Is there any way to bypass or replace Entity Framework's logic for verifying a foreign key exists? Does the EF architecture allow me to replace/modify this undesirable behavior? I feel like adding enough logic into repositories to "fake it" would be having to re-implement a significant fraction of EF functionality.

Configuring Entity Framework on a Database Undefined Foreign Key Relationships

For a variety of reasons the database that I'm working on (SQL Server 2005) doesn't have any relationships defined. Every table has a primary key. And most tables have at least one foreign key, however we've never configured the constraints.
Can anyone tell me the steps that I should take to inform Entity Framework of the underlying relationships between tables? Is there a particular document that describes this process?
You will need to manually create the associations between the tables in your EF model.
In the Entity Framework designer surface, you basically right-click on your table and from the context menu, you need to choose the "Add -> Association" option. In the dialog box that pops up, you can establish the association between your two tables - even without foreign key relationship in the underlying database.
Marc

Polymorphic associations in LINQ to SQL

Does LINQ to SQL provide out-of-the-box polymorphic associations as ruby on rails active record does? If not, is there any workaround to manually map those associations?
Agreed. I found no possible way of doing this nor using the designer nor by hand appending class/method attributes. Moreover is not possible to have foreign key constraints for polymorphic associations. I discarded this option, thanks.
EDITTED
SQL Server won't allow you to have a foreign key relationship on a column that is not a primary key or doesn't have a unique constraint (or index) on it. There doesn't seem to be any restriction on having multiple tables use the same column in the child table as the foreign key. The DBML designer does discover these relationships and will create associations to both parent tables when you import the table. It appears however, that the designer-generated code will only be generated for one of the associations. That is, the designer shows the associations properly, but the code for all but one of them is omitted. Further, the extensibility methods and property settors don't seem to get defined properly in the designer-generated code either.
The same seems to be true if you add the associations by hand in the designer. Only one of the actual associations is implemented in the code and the other parent class's code seems irretrievably broken. It's possible that you may be able to use partial class implementations to add in the required functionality to match what the designer would generate, but I haven't tried this.
Also, LINQ2SQL doesn't support many-to-many relationships out of the box. You're limited to 1-1 and 1-many without writing the code yourself.

Categories