visit Epsilon Minded Gregory Mazarakis | .net

Cross-Sync alfa-2 released...

by Gregory Mazarakis 31. January 2011 03:29

The latest stable alfa release of Cross-Sync has been published under version number 0.8.2.0.653-alfa2, and can be downloaded from our usual location here. Registered testers should download this new release. Please safeguard your information and data prior installation and use of this alfa version there the applied changes could be harmfull.

In case a new test account is required send me an email... (greg@mazarakis.org)

Cross-Sync.alfa3 is scheduled for the end of februari.

Thanks for the support!!!

Tags: , ,

.net | Cross-Sync

Entity Framework and many-to-many relations

by Gregory Mazarakis 22. April 2010 19:39

Yesterday I came across an issue in the Entity Framework that really frustrated me for a couple of hours. I decided to leave it in peace until I was able to think clearly again. Today I went back to tackle the issue and I was finally able to find a solution to my problem.

Issue

I have three tables in place, one describing a user account, one describing an in installation and finally on describing the relation between them. Both the UserAccount table and the Installation table have a primary key set on an identity column named after the table (UserAccountId and InstallationId). The identity column is set to increment with 1 step at the insert of a row. The UserAccountInstallation table provides the mapping between user accounts and installations:

image

no rocket science here… the issue now is that whenever I try to insert a new installation object into the database, I gracefully receive a System.Data.UpdateException exception, stating in the message:

A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple server-generated columns.

Solution

It seems that Visual Studio 2010 RC causes the problem. When creating/refreshing an entity model in the visual studio IDE, the columns that are marked as identity columns in the database are given the attribute “StoreGeneratedPattern” with value “identity” in the mapping:

UserAccount 

<EntityType Name="UserAccount">
  <Key>
    <PropertyRef Name="UserAccountId" />
  </Key>
  <Property Name="UserAccountId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
  <Property Name="SyncServerId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
  <Property Name="Title" Type="nvarchar" MaxLength="8" />
  <Property Name="FirstName" Type="nvarchar" Nullable="false" MaxLength="50" />
  <Property Name="MiddleName" Type="nvarchar" MaxLength="50" />
  <Property Name="LastName" Type="nvarchar" Nullable="false" MaxLength="50" />
  <Property Name="Suffix" Type="nvarchar" MaxLength="10" />
  <Property Name="Username" Type="nvarchar" Nullable="false" MaxLength="100" />
  <Property Name="CultureCode" Type="nvarchar" Nullable="false" MaxLength="5" />
  <Property Name="IsAdmin" Type="tinyint" Nullable="false" />
  <Property Name="NameStyle" Type="bit" Nullable="false" />
  <Property Name="EmailAddress" Type="nvarchar" MaxLength="50" />
  <Property Name="EmailPromotion" Type="tinyint" Nullable="false" />
  <Property Name="Phone" Type="nvarchar" MaxLength="25" />
  <Property Name="PasswordHash" Type="varchar" Nullable="false" MaxLength="128" />
  <Property Name="AccountNumber" Type="varchar" Nullable="false" MaxLength="10" StoreGeneratedPattern="Computed" />
  <Property Name="ModifiedDate" Type="datetime" Nullable="false" />
</EntityType>

Installation 

<EntityType Name="Installation">
  <Key>
    <PropertyRef Name="InstallationId" />
  </Key>
  <Property Type="Int32" Name="InstallationId" Nullable="false" a:StoreGeneratedPattern="Identity" xmlns:a="
http://schemas.microsoft.com/ado/2009/02/edm/annotation" />
  <Property Type="String" Name="Identifier" Nullable="false" MaxLength="38" FixedLength="true" Unicode="false" />
  <Property Type="Byte" Name="IsActiveCode" Nullable="false" />
</EntityType>

UserAccountInstallation 

<EntityType Name="UserAccountInstallation">
  <Key>
    <PropertyRef Name="UserAccountId" />
    <PropertyRef Name="InstallationId" />
  </Key>
  <Property Name="UserAccountId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
  <Property Name="InstallationId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
</EntityType>

The issue is caused because the columns in the mapping table are also given the same attribute and value, resulting in the database trying to auto-increment a column that is not configured to do so! The solution is to dive into the XML representation of the entity model and remove the attribute from the properties where it is not applicable.

UserAccountInstallation 

<EntityType Name="UserAccountInstallation">
  <Key>
    <PropertyRef Name="UserAccountId" />
    <PropertyRef Name="InstallationId" />
  </Key>
  <Property Name="UserAccountId" Type="int" Nullable="false" />
  <Property Name="InstallationId" Type="int" Nullable="false" />
</EntityType>

Even further… my UserAccount table contains a reference to a another table that holds server configuration. This is a many-to-1 relationship as many user accounts can be member of the same server. The server is represented by its Id (auto-generated), in column SyncServerId… it seems that this column is also given the wrong attributes and I can fairly assume now that I will run again into the same issue when I try to include a new user account.

Is think the issue will be caused with every association created by the wizard where an endpoint is an identity column in a table.

In the mean time I found out that the problem is already brought to the attention of Microsoft: https://connect.microsoft.com/data/feedback/details/540058

Tags: , ,

Development | .net | issues

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Gregory Says...

..."Find something you love to do and you'll never have to work a day in your life"...

Gregory Mazarakis

me...

Application/Solution Developer and Project Manager in a wide variety of business applications, driven by the need to obtain knowledge. Particularly interested in client/server applications, web applications and relational database design using MS-SQL Server or other products. Strong analytical and communication skills are pushing my career towards Project Management and a more abstract approach of the used technologies.

Mother tongue is Greek and Greece was, is and will always be one of my top passions. Other languages used on a daily basis are Dutch and English...

thank you for visiting!