Home » Entity Framework

If you have a parent->child relationship between two of your classes and you delete the parent, you may want to delete all children. To do, you need to have a “DELETE CASCADE” statement on your foreign key. Here is a simple example: CREATE TABLE [Tax]( [ID] [int] IDENTITY(1,1) NOT NULL, [Description] [nvarchar](100) NULL, CONSTRAINT [PK_Parent]...
This situation occur if you have already the object inside the DbContext and you try to Attach your instance. Most of the time you can manage it but just remove the Attach method but in some case that you might not know if the instance is inside the DbContext you may would like to check it before attaching. This can be done with the help of DbSet and...
In a previous post concerning the IValidatableObject with Asp.Net MV3 we have discussed the powerful of the IValidatableObject interface with MVC framework which is a fast solution to handle error in the model, error in the model binding and error in the controller. What is great by using Microsoft ecosystem is that most of the framework work well...
One of my team mate would like to query some data with Linq but has always an error message saying that the Linq to Entity couldn’t find the property desired. In fact, the exception was relevant because the property was made up in the model and were using two others properties available from the database. To figure out more about the problematic...
Having to update an object from an edit action is pretty standard. In fact, it’s also very straight forward with basic object (without containing other object). You could use the FormCollection way to do it: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(FormCollection form) { // Get movie to update var id = Int32.Parse(form["id"]); ...
If you are using 1 dbcontext per repository, you may end by having problem with your reference between each of your object. Let say that you have an object A with an object B and A is handled by RepoA and you change the object B which is handled by the context inside repoB than you won’t have any changes done. This is because the dbcontext contain...
Linq to Entity doesn’t execute directly the SQL query to the database when the query is done. This is called Deffered execution. The SQL will be executed when the code will loop through it with a foreach or if the code use .ToArray(), .ToList(), .ToDictionary() or .ToLookup(). Here is an example, mostly taken from MSDN showing when the data is...
If you have a relationship of zero to many your master object won’t have in the database any reference to the detail object. It’s the detail object that has foreign key to the master. But, on the C# class side, the master will have a collection of the detail. To make this work, you need to have a repository that will do the include correctly...
The property ProxyCreationEnabled is by default to True. This property is required to be able to do LazyLoading and also to keep track of changes to the object. But, in some situation, you may need to set it up to false. One case if to gain on performance. Entity Framework will generate a proxy class which contain some overhead that will contains the...
Entity Framework version 4.3 (and since 4.1) can throw the exception that the model and the database cannot be mapped correctly. In fact, EF is saying that the model has changed since the database has been created. The model backing the ‘MyContext’ context has changed since the database was created. Consider using Code First Migrations to...
wordpress themplates