In the screenshot below is an Entity (URL) in my model. The ParentId field is a self-referencing FK (points to Url.Id). You can see this navigation at the bottom of the screenshot.
In my SQL and my DB, which I generate the EDMX from, the self-referencing FK is called FK_Urls_Parent:
-- Creating foreign key on [ParentId] in table 'Urls'
ALTER TABLE [Urls]
ADD CONSTRAINT [FK_Urls_Parent]
FOREIGN KEY ([ParentId])
REFERENCES [Urls]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
My questions are:
Why did EF generate Urls1 and Url1 just from that one FK? Url1 is a 0 or 1 property that is 'FromRole' Urls1. Urls1 is 'FromRole' Urls 'ToRole' Urls1. It seems like EF is making a navigation property that is the exact same as Url table. Why would it do this and can I do something to make it just generate the one, desired Navigation property: Urls1?
Okay, so not quite as important, but can I control the name of the Navigation property based on the FK name or something in the DB? I hate that it names it 'Url1'. I would prefer 'Parent', but don't want to have to manually change it in the designer every time I regenerate the model.
Thanks.
It is modeling both sides of the relationship. In other words, one of the properties will be the entry being pointed to by this entry's ParentId. The other will be the entry(s) whose ParentId field points to this entry. You can disable one side in the relationship properties, or rename them to make sense. Such as, for instance, ParentUrl and ChildUrls.
I'm not 100% certain how to get to the property relation dialog without opening the program myself, which I can't right now. I do know for me it appears in the (by default) bottom-right properties window when the link is selected.
As far as making this change somehow permanent across model regenerations, I know of no way to do this. Hopefully someone else will know, because it would save me a lot of time too!
Related
I am working on a simple database and to be specific here is the model generated by database first approach (Visual Studio 2017 Community, Entity Framework 6.2):
Generated Database Model
I'd like the UserMessage table to be able to point to itself with a field named AnswerId, this is a nullable foreign key referencing its primary key. Again, to be specific, here is the part where I create the table:
UserMessage table script
My problem is that when Entity Framework generates the classes based on the existing database everything goes fine except for this particular table, in which EF suggests (I don't know why) that the UserMessage table has a multiplicity of 0..1 - * to itself while it should 1 - 0..1 (because a message may have a direct answer, but not more than 1, though that message, which is the answer, could also have an answer, so it's just like a linked list).
Here is the generated class: UserMessage generated class
To sum up the whole thing: I'd like to know why Entity Framework generates my class the way it does, and how could I make it generate it so that I only have a virtual property pointing to the answer (in case it has one), but not a collection.
Thank you for your answers!
I think what you're seeing is a correct interpretation by EntityFramework.
UserMessage1 represents a collection of all the UserMessages that have references to the parent as their answer. I understand that you probably won't use that collection for anything but it's not wrong that it's there. UserMessage2 seems to be the property you're looking for. Maybe you could rename those properties in the diagram so they're not confusing.
UserMessages1 = MessagesThatReferenceMe
UserMessages2 = the Message that I may or may not reference
I don't see how you can stop EF from generating this collection. I think if you delete the property in the diagram you will have to delete it every time you update the diagram.
Maybe try deleting the 2nd UserMessage navigation property in your model.
I need a table called Loan.
The columns of this table will be:
Id, Type, BorrowId, Description
The trick here is that Type field will determine whether the borrower is an Employee or a Customer and then the BorrowerId will either be an Id from the Employee table or an Id from the Customer table.
Is this bad design as far as Entity Framework is concerned? The reason I ask is because it seems like I won't be able to create a Borrower Navigation property on the Loan table since the Id can be from two tables.
Does anyone have a solution for this? Like how I can change my data models to work with Navigation properties.
A simple answer to your question is "Yes it's a bad design". Referential Integrity should be strictly enforced and when you remove that ability by alternating the reference you create a window for errors. If you want two options create two columns, and create foreign keys on each to the tables they reference. Then your application will be effectively foolproof. :D
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 generated an Entity Framework Model (4.0) from my database. I did not design the database and do not have any control over the schema, but there are a few tables that do not have foreign key constraints defined, but there is an implicit relationship defined.
For example:
I have a table called People that has the following columns:
GenderID
RaceID
There are tables for both Gender and Race but there is no foreign key in the People table.
When I imported the model it did not add Navigation Properties for these relationships. I tried to add it manually but From Role and To Role are disabled. I'm not sure how to add the relationship myself. How do I do this?
Yup - it's not that straightforward.
Here's what you do:
1 - Right click on the designer, Add -> Association
2 - Setup the association and cardinalities (People *..1 Gender, People *..1 Race)
3 - Go into the Model Browser -> Associations
4 - Right click on your newly created associations, click Properties
5 - Here you need to setup the endpoints for the key and cascade options. Make sure you get the endpoints correct. You can also setup a referential constraint here for your implicit navigational property.
6 - Map the navigational property to the relevant tables/fields.
7 - Validate your model, cross your fingers.
I came across this blog post which proposes the following solution, which worked great for me (unfortunately I could not get RPM1984's to work in my situation).
Add an Association via designer background right click contextual menu
Set up your Association (be sure to uncheck creation of foreign key)
Right click on the association and choose Properties
Click on the ... button for Referential Constraint
Set up the relation between the keys within
Verify (from the designer contextual menu)
???
Profit!
In the L2S designer I have dropped a table and a view. I tried adding an association between the 2 on their primary keys. This should be a one-to-one relationship, so I set the cardinality property of the association as such.
But, when coding I can't access the child property.
Any suggestions?
EditI just created a view in sql server that has the fields I want, and dropped that into L2S. Much simpler.
In my experience Linq To SQL requires the Primary/Foreign key relationships established in the DB. Otherwise, you'll have to do a join.
Not sure if this applies to your situation, but it may...
Linq To SQL Without Explicit Foreign Key Relationships
UPDATE:
It appears that you can get what you're looking for without establishing the Primary/Foreign key relationships in the database. In the LinqToSQL designer set primary keys on both columns on each table. I also had to turn off Auto-Sync on Parent Table's column that I assigned as a primary key.
Auto-Sync is described here..
Instructs the common language runtime (CLR) to retrieve the value after an insert or update operation.
Link:
http://msdn.microsoft.com/en-us/library/bb386971.aspx
We had this problem with views.
We simply defined the keys in the DBML editor and the property was finally defined.
Did you disable (set to false) either the child or parent property?
Did you perhaps mapped the one-to-one relationship the wrong way around (like I did less than an hour ago)?