.Net core 2.1.
Asset Table -> Id, Name, Path, AssetType(Enum -> user, company), ParentEntityId
I have Asset table that contains media files for multiple other tables( e.g user, companies, departments ). Since asset remain exactly same for all entities it is rational to create it once and define relationships dynamically. So entities are fetched based on AssetType and ParentEntityId, which means its not possible to define relationships for fetching child entities and using default functions like Including and user.Asset wont work.
So my question is
Does .Net core provide any out of box way to handle dynamic entities
loading.
If not whats the best approach to handle such scenario.
User has images or documents and same goes for companies. Before tables were designed like this.
User -> UserAsset
Company -> CompanyAsset
while both UserAsset and CompanyAsset both are replica with only foreign key difference as UserAsset has UserId and CompanyAsset has CompanyId. More entites are coming into system that are going to have same asset requirements. If continue same path there will be 7 Asset tables with exact same data with only difference of Foregin Key. So I thought if I could merge them together in order to avoid having multiple tables. That's why having single asset table makes more sense.
Since asset remain exactly same for all entities it is rational to create it once and define relationships dynamically.
Ah... no. I don't know who designed your database, but it's wrong. Period. That's not a database design to any form of normalization. It does not have foreign keys between entities that are meant to be related.
You should have one asset table. That has an AssetId primary key.
If you want a company to be connected to assets, it can either have an AssetId field foreign key (if it has 0-1 assets) or you can have another table with CompanyId and AssetId as foreign keys (if it has 0-n assets or even n-m when an asset can belong to multiple companies). If users have assets, too, you create a user table just like you did with the company table and reference the same assets table.
With a proper database design, Entity framework will work as desired.
Related
I am working on a project where I may not alter the database in any way (unfortunately). I have started the project using Entity Framework and this has worked for the first few objects that I need. Now I have come across two scenarios that I am not sure how to accommodate.
Tables without a primary key defined.
Tables using the suffix of the table name as a field.
For the first, I get an error about reviewing my schema and uncommenting the proper area of the edmx file. The table has a field that acts as primary key but is not designated as not null and does not have a primary key created for it.
For the second, there are several tables with names like order1, order2, order3 etc where the table that needs to be accessed would be a parameter of my access methods.
My thought is that it would be simplest to just manually write the SQL and bind the data to models. If I go that route would I even use EF or do I just create a new database connection? what would be the 'proper' way to go about that?
I'm relatively new to software development and i'm currently finishing up a project that I'm hoping to show at future job interviews. In this project, using code-first I want to set up a many-to-many relationship between an ApplicationUser and a domain Entity and have the resulting associative table persist other information that pertains to this association. The idea is that a user would be able to like many images and an image would be able to have many users like it. The problem that I've come across is that the Identity Database and the domain database are not the same. So my questions are:
1) How would I be able to create the many-to-many association between the ApplicationUsers and the Photos if both Entities are persisted in different SQL Server Databases?
2) How could I add a field to store extra information (a bool to account for whether the user liked the image or not) in that resulting associative table?
Thanks in advance for the answers, I'm usually able to find answers on my own but this one seems to be a little off the beaten track.
Create a ApplicationUser table in the domain database and have your image/user junction table reference that. Every time a user logs into your application, insert/update that user record with the relevant information from your identity database. If your identity database serves only the single application, you can combine the two databases.
I have a database with two tables, customer and account. Because multiple customers can exist on multiple accounts this is a many to many design.
This is how I designed it in SQL
This works quite nicely as Entity Framework picks up that its a mapping table and just maps Customer to Account as lists on each and hiding the mapping table. Brilliant!
I would like to extend this further to add preferences to a mapping between a customer and an account like such:
Am I right in assuming this is not possible? I have tried adding it to EF model but instead it brings back the mapping table.
Anyone else had any luck with this?
What you have in your first case is a simple many to many relation table, which in Entity Framework results in the collections of entities on one another.
If you want to have a relation to the Preference from your CustomerAccount relation table, the relation becomes complex and it cannot be depicted in the simple relation lists anymore. You need add an entity for your relation CustomerAccount which will have foreign keys on Customer, Account and Preference.
Okay. assume I have structure:
School -> students -> StudentParents <- parents -> address
School can have many students, students can be relatives and have the same set of parents (may-to-many). Each parent can have multiple addresses.
Assume that students who have the same set of parents cannot study in different schools.
If given school_Id =5, I want to remove this school and all related records.
How to do this easily in Entity Framework 4?
Answer for your question would be same as this question.
You are trying to solve the problem in the wrong layer. You need to
reconsider your database design specially how you maintain the
referential integrity.
You need to set the "CASCADE DELETE"s of the foreign keys and reflect
that in your Entity Model. Then the database will make the necessary
changes to maintain the referential integrity when you delete that
entity.
Entity framework cannot delete data from database that is not instantiated as object in memory. This means you would need to load school data, all students data, all students parent data and so on, and then you would need to manually delete all the data.
This seems like a lot of work to do, so you may want to take another approach to this problem - delete all this data using stored procedure on database that is mapped to ObjectContext, this would perform better since you would not need to get all the data into memory.
But this also seems troublesome. The best approach would be to create Cascade delete constrain on database and map it also in entity framework's model. This has two advantages - you would need to only load school data and after it is deleted from model, it would be deleted from database and cascade delete would remove all referencing data. But if you have school and students data already in memory, EF will take care of marking those objects from memory as deleted, which will make your data consistent with database state.
The best resolution to this problem depends on whether you may or may not modify database. If you can - go for cascade delete. If you cannot - I would recommend stored procedure approach as better performing (assuming performance is an issue and there is lots of students, parents etc. in database).
I'm working on a C# WPF app with a MySQL backend using Entity Data Model in VS2010 (.NET 4). I have the following simple test database schema (ignore StudentCourse.Grade for now):
Students are assigned to courses with a many-to-many relationship via a connecting table. In EDM this is represented as two entities linked by an association mapped to the connecting table:
So far so good.
Now let's say that each student gets a final grade for each course he/she takes which I've stored in the StudentCourse table as Grade. My problems is somehow getting the grade in the EDM. I've tried creating a new StudentCourse entity mapped to the StudentCourse table but it's set to read-only because it has no id. Adding an id to the table causes Visual Studio to whine because the id field is not mapped in the association(which I don't understand at all).
How is a relationship like this mapped in EDM? I'm open to changing the database schema if need be.
Edit in reply to Ucodia:
I don't know. As far as UI goes, grades should be easily accessible for a student or for a course. I'm open to suggestions.
You need to add Grade to the link table and delete the many-to-many relationship. Then regenerate the model from the database. You will find that the StudentCourse table will show in the EDM. When there are additional fields in the link table, the link table will appear in the model.
Why would you not just store the Grade on the Course entity?
Apparently your primary concern is to have something like the Model you designed in EDM designer. If only the object model matters to you, whatever the database looks like, then I would suggest you having a look at EF Code First.
Have a look at this article: EF Code First Walkthrough