Entity Framework and soft deleted records - entity-framework

I'm developing WCF service which is using entity framework as data source. Almost all is ok except problem with deleted records. In our database we're using soft delete (mark record attribute IsDeleted = true). My question how to exclude soft deleted records from entity set?
For example, entity "A" has entity set "Bs" (FK to table "B").
How to make that "Bs" entity set only contains from records which is not deleted?
Thank you

I have written a post about this topic, hope it helps.
http://blog.jorgef.net/2010/12/ef-soft-delete.html

One way would be to use a defining query. But we typically handle this in the Repository, since we actually do want to materialize "soft deleted" entities in rare cases.

You could map you EF entities to views instead of tables
CREATE VIEW vw_Currency AS
SELECT
*
FROM
Currency c
WHERE
c.IsAKDeleted=0
I have worked on a system which used this approach but it was not based on EF. I have not tried it with EF

Related

How do I create a read-only entity?

I have a web application using J2EE + Spring and a MySQL database. I need one entity which will be read-only. I have one main table with products, and they are only to read. There should be no insertion of new records and no updates currently.
The entity class should only read data and pass the entities forward (other entities are tables like order, shipments etc.).
Is there any solution for this? Does anyone have the same issue? Thanks for the help.
If you don't change an object, it will never be updated.
If you are using EclipseLink you can use the #ReadOnly annotation to mark something as read-only.

Change tracking with EF over time?

I'd like to know what is the best practice to track and/or persist changes over time if I use EF. I'd like to get started with EF for a new project. What I need is a kind of change history.
That's how I did it before: If a record was created it was saved with an ID and with the same ID as InvariantID. If the record was updated i marked it as deleted and created a new record with the new values and a new ID but the same InvariantID. Like this I always had my current record but a history of changes as well.
This works perfectly fine for my scenarios. The amount of historical records is not an issue because I use this usually only for data that's not changing very often.
Is this build in EF somehow or what's the best way to get this behavior for EF?
No it is not build into EF and it will not work this way. I even don't think that it is a good approach on the database level because it makes referential integrity very complex.
With EF this will work only if you use following approach:
You will use conditional mapping for your entity - condition will be IsDeleted = 0. It will ensure that only non deleted entities will be used in queries.
You will have mapped stored procedure for delete operation to correctly set IsDeleted = 1 instead of really deleting the record
You will have to manually call DeleteObject to delete your record and after that you will insert new record - the reason is that EF is not able to deal with scenario where entity change its PK value during update.
Your entities will not be able to participate in relations unless you manually rebuild referential integrity with some other stored procedure
You will need stored procedure to query historical (deleted) records

Delete object and all its child objects in Entity Framework?

I've been trying to find the answer to this question here. Several people seem to ask similar things, but I don't get the answers. I have an EF entity with a bunch of child entities (one-to-many relationship). I want to be able to delete the "parent" entity and have all the child entities deleted at the same time.
Some people mention "Cascade Delete" should be set on both EF model and database (Sql Server in my case). The problem is:
I have absolutely no idea how to do this (seems to be implied in those answers that you should know, but sorry...)
I have a feeling I've run into a similar problems before and found an answer somewhere that was simpler than setting this Cascade Delete. I may be wrong, maybe it is the only way, but if there is a simpler solution I'd like to know.
In either case, a clear example of how to get this working would be greatly appreciated!
In SQL Managment Studio go to your database and find the table where there should be a foreign key. Add a foreign key to the table pointing to the other table. I assume you know how to setup a foreign key. In the foreign key setup at the bottom of the dialog window you'll see a Delete property. Set it to Cascade. This will cause any dependent rows to be deleted whenever the parent row is deleted. Then go and update your data model in Visual Studio. Everything should be setup for you now.
Here is some relevant documentation on MSDN. Note though that there appears to be an error in the example. I received the following error from the EDMX designer when using this configuration.
Operations cannot be specified on ends with multiplicity '*'.
You should set the OnDelete property to Cascade for the end will be triggering deletes on the other end.
As an example, in a relationship involving customers and orders where you would like to have a customer's orders deleted along with the customer, you should set the OnDelete property for the Customer role to Cascade.
Note that only objects that have been loaded into the ObjectContext will be affected by a cascading delete. You will be relying on the cascading delete that you set in the database to look after any other records.

Getting Error 3007 when I add my Entity Model

I am getting an error 3007 when I add my entity model to my solution.
I found these links:
Good explination
Short answer
About this error:
Error 1 Error 3007: Problem in Mapping
Fragments starting at lines 89, 94:
Non-Primary-Key column(s) [Person_ID]
are being mapped in both fragments to
different conceptual side properties -
data inconsistency is possible because
the corresponding conceptual side
properties can be independently
modified.
Their Answer: I agree with their conclusion that by simply deleting the Scalar Property Person_ID and leave the Navigation Property my problem is fixed. However this is not very scalable since I am dynamically building my database and my entity is updated very often. I dont want to have to go through and clean up my entity every time I update it.
My Question: Is there a way to fix the error by correcting the way EF builds the entity? Or is there a way to remove the Scalar Property through code? Perhaps there is even a few options that I am overlooking.
Try to remove foreign property column from Entity set using entity model design it will solve your problem
For example
We have two tables one is customer and other one is order, using entity model design we added association between customers and orders when we do this Ado.net entity framework i will add navigation properties to both below tables.
Like
Customer.Orders - Here order is list
Order.Customer
One - Many relation.
So we need to remove property from with name CustomerId[Foreign key column] from Order entity set.
For reference:
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2823634f-9dd1-4547-93b5-17bb8a882ac2/
My experience with EF v1 is similar to yours. When the EDM is generated incorrectly and you can't work around the issue, you have to manually edit the EDM. EF v.Next (Entity Framework v4 I believe) will support "Code Only" Entity Data Models, and the EDM designer is supposed to be much better. One or the other improvement should make our lives easier. Until then...

Entity Framework map multiple tables to one entity

I have a database with a table for active orders and one for inactive orders. I would like to model this in Entity Framework as one entity called Orders. I also need a way to determine if an order in this collection is active or not, preferably by having a status property on the entity that is set according to what table it is in. Is there anyway to do this using Entity Framework 1. What about in Entity Framework 4?
You could create a view in your DB and generate the entity from that.
Take a look at the Table Per Concrete Type inheritance.
It is described here in ADO.NET Team Blog.
I think this is what you are looking for: How to: Define a Model with Multiple Entity Sets per Type (Entity Framework)
"The Entity Data Model (EDM) allows an entity type to be included by multiple entity sets within a single entity container or for an entity type to be included in entity sets in multiple entity containers. Defining multiple entity sets per type (MEST) allows users to streamline their code when databases have partitioning or other such scenarios where multiple tables have the same structure."
If I am understanding you correctly both active and inactive orders would share the same properties (for example: both would have a decimal "amount" property) if this is the case then in EF 1, I am pretty certain this is not possible. I think you will have to fall back to Mapping your entities to a POCO Orders object.
A good way to do one entity that shares multiple tables is to use Entity Splitting. MSDN has a very simple tutorial that walks you through the process which is very easy, however, you may need to reshape your data model: http://msdn.microsoft.com/en-us/data/jj715646.aspx