The ADO.NET Entity Framework and Error 2048

The ADO.NET Entity Framework (AEF) v1.0 is part of Visual Studio 2008 SP1. The AEF examines a SQL database and creates wrapping C# code that can be used in an application to perform insert, update, create, and delete operations on the database. Yesterday I ran into an interesting error and an interesting solution. I created a dbCars DB with two tables, tblMakes ("Audi", "BMW", etc.) and tblModels ("A4", "328i", etc.) The tblModels table has a foreign key make_id into tblMakes, with an FK constraint named fk_tblModels_tblMakes. In AEF v1.0 if you want to use any stored procedures with a table, you must implement all three of insert, update, and delete procs. Anyway, after doing this and compiling a C# console application I got: Error "2048: The EntitySet ‘tblModels’ includes function mappings for AssociationSet ‘fk_tblModels_tblMakes’, but none exists in element ‘DeleteFunction’ for type ‘dbCarsModel.tblModels’. AssociationSets must be consistently mapped for all operations." I beat my head on this for a while but a couple of guys on the AEF team, Daniel and Michael, explained. Basically, the FK constraint referred to keys in both tblMakes and tblModels but my usp_DeleteModel only uses the model key as an input parameter. One solution is to recast the usp_DeleteModel to accept a dummy input parameter of the make_id. That worked but just didn’t feel quite right. A second solution is to open the Model1.edmx file with an XML editor and place this mapping code into the SSDL section:
<Function Name="Custom_DeleteModel" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" >
     EXECUTE dbcars.usp_DeleteModel @model_id
  <Parameter Name="make_id" Type="char" Mode="In" />
  <Parameter Name="model_id" Type="int" Mode="In" />
Then, after going back to the visual designer view of the .edmx file I remapped the DeleteFunction entry to the newly defined Custom_DeleteModel (which automatically appeared in the GUI dropdown control). The XML mapping code above has the dummy make_id parameter which keeps the compiler happy, and calls the usp_DeleteModel stored procedure using just the natural model_id parameter. Cool. Here are the particulars:
create database dbCars
use dbCars
create table tblMakes — ‘Audi’, ‘BMW’, etc.
  make_id nchar(3) primary key,
  make_name nvarchar(25) not null,
  make_country nchar(2) null,
create table tblModels — ‘A4’, ‘325i’, etc.
  model_id int primary key,
  make_id nchar(3), — foreign key
  model_name nvarchar(25) not null,
  model_type nvarchar(25) null — ‘SUV’, ‘2-door coupe’, etc.
alter table tblModels add constraint fk_tblModels_tblMakes foreign key(make_id) references tblMakes(make_id)
— create procedure usp_InsertMake (here)
— create procedure usp_InsertModel (here)
— create procedure usp_UpdateMake (here)
— create procedure usp_UpdateModel (here)
create procedure usp_DeleteMake
 @make_id nchar(3)
 — delete all associated Models first
 delete from tblModels where tblModels.make_id = @make_id
 — now can delete the Make
 delete from tblMakes where make_id = @make_id
create procedure usp_DeleteModel
 @model_id int
 delete from tblModels where model_id = @model_id
Like many v1.0 products the AEF has its glitches but I bet that this one at least will be fixed with v2.0 which I think will ship with .NET Framework 4.0 and Visual Studio 2010.
This entry was posted in Software Test Automation. Bookmark the permalink.