Im trying to generate a model from a database. The model is being generated but all the foreign key fields (that should be only navigation properties) are being mapped as Navigation Property and a scalar property (IN THE MODEL).
I know the foreign key is added in the end of the multiplicity on database but whenever I generate a database from model, all the entity classes doesn't contain that field.
Is there a way to ignore this field generating the model from database?
When you generate the model from the database there is a check box called Include foreign key columns in the model. This check box is by default selected. If you deselect it your model will not contain foreign key properties but just navigation properties.
Related
I am learning MVC and I'm creating a project in which I'm Display Information like GSMarena.com phone description. I have created separate tables for Technology, Design, Display. I have used their Primary Key as Foreign key in Products Table but when I generate model in edmx file it creates properties like Design, Design1, Display, Display1 like shown in figure. I'm not able to get the reason behind this as on product create view I need all these properties for product creation.
Why is this happening and how it will effect my project?
With respect to your comment
...when I generate model in edmx file it creates properties like
Design, Design1,...
The Design1 property is the actual table. This means that from the Product table you can access the fields of the related Design table, for example you could type myProduct.Design1.Dimensions to access the Dimensions field from an instance of Products.
The Design property is the actual numeric value of the column Id of the Design table.
I hope this is clear enough.
In the top part of you Product model, you've created fields for Brand, Camera, Connectivity, Design, Display, Hardware and Technology. These are not Foreign Keys
So when you add the associations between the classes, it adds Navigation Properties (which are Foreign Keys) to the table, with the next most appropriate name.
Just remove the fields from the top half of the model and rename the Navigation Properties.
I have a DBFirst EntityFramework 6.1 solution that i'm trying to generate off of. When i add a table that only contains two foreign keys the table is turned into two associations and I can not directly access the table anymore. This is neat for navigation in the code but makes it a pain in the ass to delete records from the table.
Is there a way to prevent this behavior and gain direct access to the table as an entity?
For example i am unable to remove an entry in the association because i get this error
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
For example here is how my database sees the structure.
Here is how it appears in entity framework. Notice that the CorporateDataShareVisible table is missing and instead two new associations are created.
The CorporateDataShareVisible table should be able to be deleted and added to at will but any changes i make seem to stop it from working.
Add a primary key to your table that has only foreign keys. EF uses the primary key to keep track internally of the element. Without a primary key it doesnt know which element was modified and how to send that back to your RDBMS.
I prefer surrogate keys i.e auto incrementing integers.
You can also add the primary key by making it a composite key of both the foreign keys
I have an entity called Requirements and an entity called Actions and an entity called Requirement_Fulfillment that is a composite of the Requirement and Action primary keys but also has an integer field.
To better illustrate, Requirement has a navigation property Requirement_Fulfillmments, Action also has a navigation property Requirement_Fulfillments, and Requirement_Fulfillment has 2 navigation properties for Requirement and Action.
If I want to add a Requirement_Fulfillment, and I specify the primary key so that it matches up with Requirement's and Action' primary key will it automatically be added to their navigation properties?
Or do I need to manually add it to the navigation properties of both sides or one side?
The navigation properties are just that, a link to the other entity. You wouldn't add to the navigation property, you would add to the entity itself and then access it via a navigation property.
I have a very precise gap of knowledge of the assignement of the entity keys in entity framework.
When you set a variable to become the entity key of an entity, if you are doing code first or model first it sets the column as identity but what if I assign the entity key to 0 in the default constructor of my model first class, what does EF do?
Will EF notice that it was an auto assignement and ignore the default value I set in the constructor?
Or should I never assign an entity key other than when im retrieving data?
So far I have yet to see the first scenario where there is absolutely no alternative for setting an EntityKey object. But I assume you're not referring to that but to setting a key value.
Setting a primary key value is hardly ever necessary when the key is generated by the database (identity column). If you want EF to insert an entity you don't set the primary key, but you set its EntityState to Added (either directly or indirectly).
I can think of one scenario where setting primary key values is useful: using stub entities. For example, suppose you know that object A with id value 3 must be deleted, but object A has not been fetched from the database. So there is no object which state can be changed to Deleted. In that case you can prevent a roundtrip to the database by creating a stub entity A that only has its Id value filled, attach it to a context as Deleted and save changes.
I don't want to use foreign key association to CompanyType (member that will hold the foreign key id) but prefer to use navigation property. So I removed the CompanyTypeId.
I get this exception that relates the relationship between entity Company and CompanyType:
Error 5: The element 'Principal' in
namespace
'http://schemas.microsoft.com/ado/2008/09/edm'
has incomplete content. List of
possible elements expected:
'PropertyRef' in namespace
'http://schemas.microsoft.com/ado/2008/09/edm'.
How can I remove those id's from the POCOs without getting the exception?
This is the difference between Foreign key association and Independent association. Both associations use navigation properties but only Foreign key association uses FK property as well. You can either remove them globally as #Robbie mentioned or you can change the type manually for selected relation.
Select the relation in entity framework designer
In properties remove Referential constraints
Go to Mapping window and map the relation
Here is the screen shot from one of my testing application with one-to-many relation between Order and OrderLine entities:
As you can see there is no OrderId in the OrderLine entity and referential constraints of the relation are empty. Also mapping of the relation is specified.
BUT you can't never remove Id from CompanyType. Ids (PKs) are mandatory. You can only change its accessibility in its properties.
When you imported in your Model from your DB you are asked if you want to:
"Include Foreign key columns in the model"
you need to switch this off.