Entity Framework 3.5 Not adding all Properties in Model - c#

I'm experiencing an unusual issue with Entity Framework in VS2008. The problem is when I create my Entity Model from my existing database, the designer add all the corresponding tables but it appears that it randomly omits some of my Foreign Key fields. Yet if I browse the Model in the Browser I can clearly see that the fields were included.
This became apparent when during the build I started receiving multiple errors that these fields were not mapped. This led me to manually add the fields in the designer to the corresponding tables and map them to the datasource.
After this however, I get multiple errors:
Error 102 Error 10023: Could not find the conceptual model to validate.
Error 103 Error 10024: Could not find the storage model to validate.
Error 104 Error 10025: Could not find the mapping model to validate.
Error 105 Could not find the Conceptual Schema node to embed as a resource for input file
Error 106 Could not find the Storage Schema node to embed as a resource for input file
Error 107 Could not find the Mapping node to embed as a resource for input file
When I open the edmx file in XML Editor, the file is riddled with squiggly lines.
I have successfully created the Entity Model in VS2010 without issue but our client isn't at 4.0 Framework.
I have identified others having the issue but I can't find any resources to assist on fixing the problem.
Any suggestions would be greatly appreciated.

This is probably EF1 did not support foreign keys. So for foreign key columns EF will create navigation properties but will not create foreign key properties on the Entities. You can find foreign properties in model browser but this is in the model representing the database and not the conceptual model. Note that navigation properties are modeled based on foreign keys in the store and you are not losing the relationship functionality even though you don't have foreign keys exposed. In other words - if you use navigation properties in queries they will be translated correctly to use foreign key values in the SQL queries. One thing where having foreign key properties in the conceptual model is very useful is when you would like to reason or modify a relationship without having to load the related entity - if you know the key value you just set the foreign key property to this value and you are done with it. This is impossible in EF1 - whenever you need to change the relationship or query against a related entity you first need to load said entity and use navigation property.

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.

Duplicate Record Found Error when Creating Entity

When creating an entity using the Xrm Tooling NuGet package, I receive the following error:
Duplicate Record Found for Entity: [some entity type code] with ID: [some guid]
I am copying a bunch of attributes from another record in the same table (custom entity for which I'm splitting some records out into multiple records as part of a cleanup effort), including the primary field. However, there are no alternate keys defined for this particular entity and no duplicate detection rules enabled either.
Any idea why I'd be receiving this error?
For me personally, this was because I forgot to filter out the primary key attribute from the collection of attributes I was copying to the cloned entity.
Note that typically a better approach would be to setup a reflexive relationship, map attributes through native field mappings, and then use the InitializeFromRequest but this was a quick migration script.
One other thing that could potentially cause this error we've found: if you don't include "RequireNewInstance=true" in your connection string, you may accidentally end up with a cached connection to another org and therefore encounter duplicates if you're performing the same updates against multiple orgs.

Relation chanage not getting reflected in entity model

Am very new to Entity Framework. I am using vs 2012 professional 2012 for building my mvc4 app.
I will try to explain problem in my limited knowledge
I have created Ado.net entity data model. After that I have chnaged db for setting relations. After "update model from database" the relations are not gettng added to model classes. So when I run my app errors like
The relationship 'x.FK_Privileges_Module' was not loaded because the type 'x.Privilege' is not available.
is shown
How can I solve this. I even tried adding manually the relation to corresponding . cs file and still getting error for every newly added relation.
How can I solve this?
You can remove entities from model designer (Press Yes when message box will ask) and then add entities again.
Note: To delete, select entities and press delete key
The edmx is not able to find a Primary Key on Privilege and therefore the foregin key is not valid.
Add a Primary Key on Privilege

Entity Framework 4.0 generating read only model when I create model from existing database

I am working on a custom ado.net provider and using that provider I am integrating Entity Framework support in Visual Studio 2010. I'm creating all possible mapping and reading all the related metadata from database for table objects. For my test, a table contains primary keys and and other fields. When I create a model from database using this table and I get a model with all column mappings and everything but I also get error messages that follow:
The model was generated with warnings or errors.
Please see the Error List for more details. These issues must be fixed before running your application.
Loading metadata from the database took 00:00:11.4799371.
Generating the model took 00:00:04.2751189.
Added the connection string to the App.Config file.
Writing the .edmx file took 00:00:00.0005060.
If I open the .edmx file with XML editor, I see the following error:
<!--Errors Found During Generation:
warning 6002: The table/view 'sqlfire.APP.CUSTOMERS' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
-->
It also added a DefiningQuery with a SELECT ... statement for the table.
I am going in a circle to find a solution and desperately looking for some help on this issue.
From the error message looks like one of your table/view doesn't have a Primary-Key. EF needs a Primary-Key in every table in order to generate Entity keys.
You may still be able to run your application, but I strongly suggest you add primary keys as warned.

Foreign key contraints interpretation problem

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.

Categories