How can I explore EF relationships and models at runtime? - c#

I have a model call "Customer" which is to be linked to several other models (tables) in Entity Framework. When deleting the said Customer, I need to show all the related records and their counts and finally list all the records before allowing the user to delete the customer.
For example I might say this Customer is related to : Orders (1), Order_Items (5) where a customer is directly linked to an Order, and then an Order is linked to Order Items, but deleting the Customer will impact records in both the tables.
Is there a way to do this in .net dynamically or will I need to have a static view where I update all this info every time I make changes to the model and relationships?

Related

C# Entity Framework Primary Key Violation

I am getting data from an external API and want to save it in SQL Server with the Entity Framework. The data I am getting is order information. The data is roughly laid out like this:
Order:
Order ID
Customer
Shipping Address
Items
Customer:
Customer ID
Customer Details
The problem is that any customer can place more than one order. My entity framework is based around the Order information. So if a customer places more than one order, I get a primary key violation because the customer is already in the database even though the order is not.
For reference: I usually add orders like so: orderContext.Orders.Add(order);
Since every order contains the customer data, I can not add an order without "adding" a customer. Even if I were to check for an existing customer, I would still have to add the record to the Orders table, which would still trigger the problem. Is there any way to tell EF to add or update each "child" class?
The example here is more simplistic than the actual data. There are several other dependencies like this, such that even if I were to switch to using the Customer class as the basis for the EF, it would still have this problem.
Change your tables like this:
add CustomerID to your Order table, this is because everyone Order has just one Customer and every Customer has many or zero Order
Order(OrderID,CustomerID,ShippingAddress)
Customer(CustomerID,...)

Entity Framework with many to many relationship generetad tables

Here's my question. I have 2 models (Person, Event) and with EF and modelbuilder I generate a booking table (with IdPerson and IdEvent as properties).
So in my DB it's correct, I have 3 tables (Person, Event and Booking) with many to many relationship. But I have only 2 models in Visual Studio (Booking doesn't exist because of the self-generated table).
With my Controller I want to write an action for the Person to suscribe to an event and I have to write on my table Booking on the DB but it doesn't exist as a model so I can't do that .
How should I proceede?
Should I create a Booking model and delete my modelbuilder?
When you are using ORMs like EF, you can sit back and let the ORM manage these middle tables.
You can use
person.Events.Add(event)
or
event.People.Add(event)
and EF handles all and inserts a row with personId and eventId in that table.
Here you can find a complete sample:
http://blogs.msdn.com/b/wriju/archive/2011/05/14/code-first-ef-4-1-building-many-to-many-relationship.aspx
I assume this is a model first approach.
The reason for having only 2 objects is that, by default, EF does not create objects for joint tables. What it does create is Navigation Property (Entity Framework - Navigation Property Basics). In one-to-many scenario, a navigation property inside a parent object contains a collection of entities in a foreign / child table. In many-to-many scenario, navigation properties of each entities will simply contain collections of its other entities.

Most suitable approachs for retrieving Master and Detail Tables in MVC4

In order to populate Dropdownlists, showing the details rather than id on Gridviews (showing City name instead of Birt city id), etc., sometimes we need to retrieve data at the same time from Master (for ex. Student) and Detail (for ex. City) tables. Could you suggest me which scenarios below is the most suitable? In addition to this I would be appreciated if you suggest other approaches provided that using Entity Framework.
1) I retrieve data from Entity tables and I use relations between master and detail tables. But in that case I need to define these relations on my DbContext and I have to populate dropdownlists by methods. But for displaying data I need to define another methods or etc. For this reason actually I do not like this approach. What do you think for this?
2) Instead of this I can use ModelView as I had used before. But, I think it is not good idea return more tables instead of one table lots of times. In addition to this, I think I need a extra definition for dropdownlist for example in an htl helper. I think it is also not handy.
3) As it is used commonly, I think getting data from entity views (database view) instead of entity table seems to be very useful. With the help of this approach, I can retrieve data from 2 tables and I can easily show this data on dropdownlists and grids without an extra effort. What do you think?
a) On the other hand, if I use a view entity instead of table entity, how can I save this entity to the database (normally I return table entity for creating/editing).
b) If this method is good, in that case I think I need extra entity definitions for the related database views in addition to the tables. For example I have Student, City entities now. But for the database View I need another 3rd entity. Could you give me a usage example for this approach?
BR.

in relation many to many is included a lot of objects

I have a many to many relation between two tables in my database, so in my edmx model I have only two tables, no the intermediate table. This tables are Movies and Genres.
When I search for some movies, I get also the related entities, so with eager loading I populate the collection Genres of my entities Movies. Is what I want.
The problem is that when in the result has more than one movie, the collection Genres of the entities MOvies are populated, but also the collection Movies of the entities Genres are populated. So when I send to the repository one movie to be updated, if I attach the the entity to the context, I attach this movie and all the movies that have the same genres that the movie that I want to update. This is because in the genres of the entities are included the movies that are of the same genres than the movie that I want to update.
So if in my first search I get a lot of movies, when I want to update one movie I send a lot of entities that I don't need to seed, so it is a lot of traffic in the network.
is there any way that when I search for results the collection of movies in the genres entities are not populated? Because I only populate the related entities of my main entity, movies, not all of them.
I am using SQLite and EF 4.4; and I try to find the mergeOption to disabled the tracking of the genres, but I don't find it.
Thanks.
When you are designing entities and relationships (logical design), you can describe a many-to-many relationship between two entities. When you need to implement these entities and their relationship in a database (physical design), you can't do it with only two tables; you really need an intermediate table to express the relationship.
A one-to-many relationship between two entities only needs a table for each entity (simple foreign key relationship). So, if your genres table was simply a list of genres for a given movie, it would be a one-to-many (movie as parent, genres as child); that would work.
However, since you've described genres to be in a many-to-many relationship with movies, it suggests that genres is to be used to supply a sort of "pick list" i.e. one row in genres is not related to any specific row in movies, but could be related to any movie (and vice-versa). To make this relationship work, you need an intermediate table, let's call it "moviegenres" which need only contain two columns - a foreign key relating to movies' primary key, and a foreign key relating to genre's primary key. This table is actually expressing your many-to-many relationship, and assigning genres to movies is then effected by adding rows to "moviegenres".
Before you can consider any issues with how your data is populating, you need to resolve the logical-to-physical design issue first.

Creating a custom entity with the Entity framework

If I have for example two entities, lets say Customers and Staff with no relation between them, is it possible to create a third entity which doesn't have a corresponding table in the database which takes some information from the first and the second entity and also one or two additional columns (for example computed columns)?
You can join entities through this: http://blogs.msdn.com/b/simonince/archive/2009/03/23/mapping-two-tables-to-one-entity-in-the-entity-framework.aspx
But I do believe they have to be related; otherwise, how would it know what information was correctly tied to each other? The only thing it could do is query all rows of one entity and match them up with all rows of the other, which is probably not what's desired.

Categories

Resources