Entity Framework Composite Key - c#

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.

Related

Dynamic models in .Net core

.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.

How does this relationship map to EDM entities?

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

Explicit many to many join table in Entity Framework 4

By default EF hides a many to many join table that does not contain additional data than the foreign keys to the joined tables.
Is it possible to tell EF (and the designer) to explicitly create the join table and make it usable in code?
Thanks
No EF designer will not add this entity for you. If you want junction table exposed you must manually delete created relation and add junction table's entity and two one-to-many FK relations. Here are related questions with step by step guide:
How to expose the join table in many to many relation
How to get Entity to Table mapping for many to many relations

Entity Framework best practice

I am developing ASP.NET (WebForms) app and using EntityFramework. I was wondering what is the best practice of using entities. Create few entities for whole database or create many entities for specialized purposes ?!
Example case is this:
I have customers table. This table have ForeignKey to customers_addresses and customers_phones tables. I got two cases on this dataset
Autocomplete customer name
Show user details
For case 1 I got one entity which have only the "name" column mapped to user to db
For case 2 I got another entity which have all data and connections between other tables.
Now I was wondering if only single entity (number 2) would be good for both cases.
What's the best practice with EF ?
I don't see a reason, in your case, to have a specialized entity for the autocomplete scenario. Since you'll be using Linq to Entities for all your querying, just make sure you are selecting just the Customer Name from the entity, instead of the entire entity itself
from Customer cust in ent.Customers
select new {
CustomerName = cust.CustomerName
};
That way you still have lightweight SQL query on the backend, but you're not polluting your models with unnecessary, "specialized" entities.
In addition, if you're using lazy loading then you don't have to worry about EF loading any related entity information unless you actually need it.

Linq to SQL many-to-many relationship without a third class

In my database I have the following tables:
Customers (ID)
Orders (ID)
CustomersOrders (CustomerID, OrderID)
How do I map Customers table to Customers class and Orders table to Orders class without creating a class CustomersOrders?
That depends on which LINQ version you're talking about.
If you're using Entity Framework 4.0 and you have no additional information in the table other than the IDs then what you are asking for should already be generated. I believe the same is true for Entity Framework 1.0.
LINQ to SQL is another story. It never handled Many-to-Many relationships well. You have to allow LINQ to SQL generate the third table and then extend the partial classes by hand in a separate file to mask away that third table. It's ugly but it works. Here's a series of blog posts that detail exactly what needs to be done:
How to implement a many-to-many relationship using Linq to SQL

Categories