Tuesday, 18 November 2014

Entity Framework Exception - A referential integrity constraint violation occurred


Entity Framework Exception - A referential integrity constraint violation occurred. When you get this type of the exception it just simply means that you need to match Ids of the related entities.

Exception: A referential integrity constraint violation occurred: The property value(s) of 'Image.Id' on one end of a relationship do not match the property value(s) of 'BaseEntity.SomeImageId' on the other end. 

A typical mistake that cause this exception is:

baseEntity.SomeImageId (type of Image)

and

Image.Id

Does not match.

So if you change the new added related entity to the negative Ids (see the related blog post), then you also need to modify the parent end of the Id (one end of the relationship as Entity Framework expression goes). So that both "ends" have the same Id value.

Entity Framework exception - adding new Detached Object to the DbContext


Entity Framework exception - adding new Detached Object to the DbContext? How to?

When you see this exception:

Exception: Attaching an entity of type 'Image' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.

This just mean that you need to properly handle detanched entities Ids. This exception means that two of your newly added entities have the same Id. Typically the value of the Id is 0.

So the solution is that you need to manually change Ids. You need to make sure that every Id from new added entity is different. Typically you should put negative Ids. Like -1, -2, -3, ...

Sunday, 24 August 2014

How to create new MS SQL virtual machine in Azure

How to create MS SQL virtual machine in Azure and how to enable MS SQL Login user.

1. Create new virtual machine with MS SQL server
2. Open a MS SQL port 1433 in azure management for the given virtual machine
3. Log in to the virtual machine
4. Open port in firewall in virtual machine Windows system - 1433
5. IMPORTANT - Set MS SQL setting to the Mixed Mode (Windows Authentication and SQL Server Authentication) (default is only Windows Auth.)

Thursday, 22 May 2014

ASP.NET MVC + T4MVC: How to generate url

If you need to generate a URL link in ASP.NET MVC within your view or controller logic with help of T4MVC, just use the following syntax:
Url.Action(MVC.Offer.Edit(offer.Id))