I have several tables whose relationships are based upon unique key constraints.
A quick example is:
VersionId, VersionName
SurveyId, SurveyName, VersionId
QuestionId, QuestionName, SurveyId, VersionId
EF doesn't currently support relationships based upon unique key constraints. In my index view for question what's the best way handle the join to survey to show the grid of questions with survey name with respect to a model?
Do I need an anonymous type? db.Questions.Include("Surveys") doesn't seem to do anything. I could use linq and make a ViewModel of the joined tables (I suspect this is the way to go), but there are so many things in EF & MVC that I thought I'd check before doing anything.
Why do you have a link to Version (i.e. VersionID) in both your survey table and your question table? Couldn't you reach the version from the question through the survey?
Also, if you have the relationship between Question and Survey is many-to-one or one-to-one (each question only has one survey) then it should be db.Questions.Include("Survey") - non-plural.
First of all, no joins (or practically any other logic) in razors views. Controller is the place to build up the ViewModel, and views are means to just present that ViewModel. As you mentioned, making ViewModel for it is only (correct) way to go. And you can fill that viewmodel by whatever method - linq is absolutely normal way to create joined data. And if you want to go further, you should place that join logic in some kind of repository, for example QuestionRepository, not in controller.
Related
I'm starting to write a project and its my first time using ASP.NET MVC and Entity Framework (since now I've used PHP for around 5 years).
I got categories and posts, each category has to have its own unique filters that can be strings or Booleans (I'll get them via textboxes or checkboxes). I'm starting to get confused when designing the entities.
I'm using code-first approach but don't know how to set-up custom fields in Entity Framework. If I'd design that in PHP and pure SQL, I think that in order to keep it perform good, I'd create extra columns on the fly (e.g. "filter_1", "filter_2") and then create mapping table that contains the description of the field, the type, etc. and I didn't figure out if this kind of implementation is possible in EF.
I've thought about some options:
- I can create the filter using many-to-many relationships when creating a filters and filters-values tables and when creating a post add the filter values into that table. The main con about it is he performance - I'll most likely have 40k+ rows and more than 20 custom filters for sure... so searching and data fetching will be too slow...
- I thought about serializing the filtered values into some form of content, for example into bytes array and then only desterilize it - but the problem is that I won't be able to search within that...
- I can use the traditional ADO.NET way in order to create my initial idea (that I've described when talking about PHP - using the "filter_N" columns) but that'd create too much mess with EF...
There's any other way to achieve my goal (to create custom filters)? any way to create custom fields using EF?
Thanks for your help!
I don't know of a good way offhand to implement this in the Entity Framework. If you really wanted to use the Entity Framework I believe you could design your database tables in such a way that you wouldn't have to create the extra columns on the fly. You could have separate tables to hold the filters and relate them back to categories.
Say I have a client dataset CDSPerson that acts as a wrapper around a Persons database table. Say I have another table, PersonBenefits, that joins 1:1 back to the Persons table.
Say I wrap a Delphi class around CDSPerson, PersonClass, and another class around CDSPersonBenefits, PersonBenefitsClass, to read and write records. PersonBenefitsClass inherits from PersonClass so it can provide data from both tables. I'd like to be able to write data back to either table through PersonBenefitsClass.
Has anyone developed a clean way to handle the SQL query, provider flags and commit logic in the inherited class so that (a) fields stay aligned with the parent class and (b) both database tables can be updated?
Is there a reference for this that I can't find? Is this just a bad idea? I'm using Delphi 2007.
If you're going to develop a business-object-to-database mapping framework, (commonly known as ORM, Object-Relational Mapper,) you're going to need to put in a bit of architecture to make relationships like this work properly. Here's one way to do it:
PersonClass and BenefitsClass both inherit from BusinessObjectClass. BusinessObjectClass is a base class that contains the general logic to interact with the dataset. It has a list object of some sort that contains a list of relation objects.
Each relation object is a special object that contains either one or a list of BusinessObjectClass descendants, plus extra data describing the foreign-key relationship between the two tables. When BusinessObjectClass does its queries and its updates, it needs to iterate through all its relation objects and have them do their own queries and updates as appropriate.
In your composite object, (PersonWithBenefitsClass,) in the constructor, call inherited and then set up a relation object that describes the related BenefitsClass. Make sure that any inserts of new objects are done in the right order to preserve referential integrity.
That's the basic idea. (One basic idea. There are probably plenty of other ways to do it.) I'll leave the details of exactly how you implement it up to you.
I use the Entity Framework 4 to migrate a legacy application to C #. The data model consists of two tables, "Appointment" and "Authorization", which are in a one to many relationship. In the conceptual model, the classes are properly represented. Also the navigation property "Appointment.Authorizations" works so far correct.
But I want the navigation property "Appointment.Authorizations" only objects of class Authorization are listed that meet certain criteria. The criteria can be very complex. Expressed in SQL, there are about 30 lines of code. Several other tables are also involved in the query.
Which method is best for now to change the behavior of the navigation property, so that only the desired selection is listed? A read only Collection would be ok.
The navigation property looks okay, what your describing is business logic keep it out of the SQL and in the C#, where you can write unit tests against this logic.
If you can provide more information about the "select" logic and provide some code we can help you get in the right direction.
I'm using MS SQL Server 2008R2, but I believe this is database agnostic.
I'm redesigning some of my sql structure, and I'm looking for the best way to set up 1 to many relationships.
I have 3 tables, Companies, Suppliers and Utilities, any of these can have a 1 to many relationship with another table called VanInfo.
A van info record can either belong to a company, supplier or utility.
I originally had a company_id in the VanInfo table that pointed to the company table, but then when I added suppliers, they needed vaninfo records as well, so I added another column in VanInfo for supplier_id, and set a constraint that either supplier_id or company_id was set and the other was null.
Now I've added Utilities, and now they need access to the VanInfo table, and I'm realizing that this is not the optimum structure.
What would be the proper way of setting up these relationships? Or should I just continue adding foreign keys to the VanInfo table? or set up some sort of cross reference table.
The application isn't technically live yet, but I want to make sure that this is set up using the best possible practices.
UPDATE:
Thank you for all the quick responses.
I've read all the suggestions, checked out all the links. My main criteria is something that would be easy to modify and maintain as clients requirements always tend to change without a lot of notice. After studying, research and planning, I'm thinking it is best to go with a cross reference table of sorts named Organizations, and 1 to 1 relationships between Companies/Utilities/Suppliers and the Organizations table, allowing a clean relationship to the Vaninfo table. This is going to be easy to maintain and still properly model my business objects.
With your example I would always go for 'some sort of cross reference table' - adding columns to the VanInfo table smells.
Ultimately you'll have more joins in your SP's but I think the overhead is worth it.
When you design a database you should not think about where the primary/foreign key goes because those are concepts that doesn’t belong to the design stage. I know it sound weird but you should not think about tables as well ! (you could implement your E/R model using XML/Files/Whatever
Sticking to E/R relationship design you should just indentify your entity (in your case Company/supplier/utilities/vanInfo) and then think about what kind of relationship there is between them(if there are any). For example you said the company can have one or more VanInfo but the Van Info can belong only to one Company. We are talking about a one – to- many relationship as you have already guessed. At this point when you “convert” you design model (a one-to many relationship) to a Database table you will know where to put the keys/ foreign keys. In the case of a one-to-Many relationship the foreign key should go to the “Many” side. In this case the van info will have a foreign keys to company (so the vaninfo table will contain the company id) . You have to follow this way for all the others tables
Have a look at the link below:
https://homepages.westminster.org.uk/it_new/BTEC%20Development/Advanced/Advanced%20Data%20Handling/ERdiagrams/build.htm
Consider making Com, Sup and Util PKs a GUID, this should be enough to solve the problem. However this sutiation may be a good indicator of poor database design, but to propose a different solution one should know more broad database context, i.e. that you are trying to achive. To me this seems like a VanInfo should be just a separate entity for each of the tables (yes, exact duplicate like Com_VanInfo, Sup_VanInfo etc), unless VanInfo isn't shared between this entities (then relationships should be inverted, i.e. Com, Sup and Util should contain FK for VanInfo).
Your database basically need normalization and I think you're database should be on its fifth normal form where you have two tables linked by one table. Please see this article, this will help you:
http://en.wikipedia.org/wiki/Fifth_normal_form
You may also want to see this, database normalization:
http://en.wikipedia.org/wiki/Database_normalization
I'm not sure if this question is non-sense or not, please tell me if so. I am wondering do I create my models like one per each table in my database, or do you do one per controller? Is there something I am missing here?
From what I read the Model is suppose to be a representation of the real business objects, so would I just want to make it one big model or divide them out based on things in the app? based on real user/client perception of the data?
Thanks for your advice.
There's nothing wrong with controllers sharing models. But trying to serve every controller with the same model doesn't make sense.
Models and controllers really are't related, nor should they be. Models also aren't directly related to how data is stored in your application.
Models encapsulate data. Their design should be dictated by the data they are encapsulating. The demands of the system dictate what models you'll need and what data they must hold.
Don't try to overthink it. For a given request, determine what you need to show in your view and how it will be displayed. Determine what an appropriate model would look like for this scenario. If one already exists, use it. If not, create a new model. Save the overengineering later when you know what your needs are and can find commonalities between models.
Models can also contain other models, that's fine. Think of a model for a sales report. You would have a model for the report which would contain not only a report name, a total, but also a collection of other models which make up the report's line items.
It depends on what you mean by "Model". If by model you mean the business rule layer of your application, then there is no relationship in terms of numbers. One model of that type is used for any amount of views you need to build.
Personally, however, I would not bind any view to any model, but create an intermediary layer called a ViewModel that essentially taylors the data from your model to fit a particular view. In that case, the relationship is one-to-one. This is essentially how Presenter patterns work. Every view is strongly typed to it's own ViewModel that is populated from the Model layer.
Models do not necessarily have a literal coorespondence with the database either. How you store data your model is different from how your "Model" uses that data.