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.
Related
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 have set of Entities generated according to DB First work flow in EntityFramework.
The Entities have in proper names so i decide to change them but when i change the Entity name, the navigation property keeps the pluralization and the singularization of the old name like the following : Two Entities (SREmp ,SRPic)
SREmp SRPic
Id id
name content
phone note
- - - - - -
SRPics SREmp
Now when i change the names of my entities to (Employee,EmpPhoto),
the navigation properties didn't change !!
As you know, when you use the DB First approach, you're using a T4 template that extracts the information from the database to create the model entities. So, the model entities will get their names and properties from the information in the databse.
So, if you need to customize the names of your properties, there are only two ways to do it, and both involve customizing the T4 template:
if you cannot modify your database in any way, because you don't want to do it or you don't have permissions, modify the T4 template by including a dictionary that maps the database table names to the desired entity names. Then, along the T4 template code, use the dictionary to create the entity names, and also the navigation property names.
if you can modify the databse, modify it by adding information about the desired entity names, navigation names and so on. You can do it in extended properties, or using naming conventions for, for example, the foreign key constraints.
The 2nd solution can be implemented in several different ways. For example, I use the FK constraint names to define the navigation properties. I name my DB FKs soemthing like FK_Employee_Pictures, so that the template create the navigation properties Employee in the pictures table, and Pictures in the employee table. This is really powerful, because you can't give precise names to your relations, with names like FK_Empoyee_FacePicture or Fk_Employee_IdCardPicture. You can also use extended properties, like the descriptions, to access them from your T4 template. You can define your own conventions, and then you have to modify the T4 template to use them.
Any other possible solution requires manual intervention, which is a very bad idea. You could find all uses of a given entity in your model project, and make a rename refactor. But that's expensive, and, if you modify your database, you'll need to update you model entities by hand again. Using any of the previous solutions you only have to update the mapping dictionary, or the DB conventions, and run the T4 template again, which is much cheaper and repeatable.
You should look for a powerfu, and easy to understand an customize template. I really like this one: EntityFramework Reverse POCO Generator.
I am working on an MVC project in Visual Studio Ultimate 2013 and I have some unwanted columns in some of my tables and I cannot really understand why is it so.
In table Categories there is the column Template_TemplateId which is probably because in the Template entity class there is a ICollection<Category> property.
I want a Template to have many categories, however a one category can be repeatedly used by many Templates so I guess the Template_TemplateId column is something I don't want there.
Can someone explain, why is this so, and what should be a right approach to have the model the way I want it?
Thank you.
Model First:
Be default, EntityFramework creates one-to-many type relationships. When you create those, you need a foreign key in the "one" side of the relationship.
This is the "Template_TemplateID" field. It is the foreign key into the "Templates" category. However, you said you wanted a many-to-many relationship, so you are absolutely right; that field is useless!
You need to specify a cardinality of "*" on both ends of the relationship, then the model will create an intermediate table allowing the many-to-many relationship. The foreign key should go away at this point.
Code First:
Same problem, but the solution is to make a collection of "Template" in categories and and a collection of "Category" in templates, thus creating the many-to-many relationship.
If you want a many-to-many relationship between Templates and Categories create two collections, on on each class - on Template, create a ICollection and on Category, create ICollection.
good morning.
i am not a computer scientist by education, so please overlook any shady term abuse.
in my framework, a base form called Record opens form RecordDetail as dialog. for example, RecordClient extends Record and contains client data and tabbed lists of client-relevant child data, and RecordDetail expands one child data row for detailed editing based wholly on reflection and display overrides stored as custom attributes used against DataContext. Record is subclassed for the application, but i see no need to subclass RecordDetail. this works except for the display of foreign key edits in the listed children.
i must use ObjectListView and Linq; i cannot use WPF/XML. because i do the serialization trick for non-continuous database connectivity, i lose foreign key objects when i clone data to manage state. ObjectListView needs the foreign key object for display and general wickedness. i know Hibernate, but Linq leaves me at a loss:
how might i access the foreign key object from within Record after its dialog to RecordDetail closes - without coupling framework and application-specific classes?
that is,
Type rowType = row.GetType();
IDomain workDamnit = (IDomain)dataContextReflectedFromRowType.GetTable(rowType).Where(x => x.PrimaryKey == 1).SingleOrDefault();
where "PrimaryKey" wraps the primary key attribute, and the fail happens between "GetTable", "Where" and "SingleOrDefault".
any perspective appreciated on this fine sunday morning.
actually, i've decided to uncle under and cache the foreign key objects in a pool accessed by the subclassed Record forms.
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!