tags:

views:

653

answers:

2

I've been struggling with a problem for the past couple days and haven't found a solution.

I have an Visual Studio solution with 2 projects, the first one is a DLL with my business objects and logic, the other project is my WinForm application, and a reference dependency on the first project.

I initially wrote the business objects with Attribute LINQ mapping and everything was working fine. Then I thought I would try external mapping. I followed a few different guides on how it should work; however, everytime I ran the code in my solution I would get a InvalidOperationException: Mapping Problem: Cannot find type 'Org.Example.System.Material' from mapping, when the WinForm attempts to create the DataContext object.

After trying different configurations in the XML file I placed a copy of the class into my GUI solution under a different namespace and it worked. My question is, is it not possible to map an object in a class library or if it is how is it accomplished.

Samples

dbmap.xml

<Database xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007" Name="Gravel_Dev"><Table Name="dbo.Materials" Member="Material">
<Type Name="Org.Example.System.Material">
  <Column Name="MaterialID" Member="MaterialID" Storage="mMaterialID" DbType="UniqueIdentifier NOT NULL" IsDbGenerated="true" IsPrimaryKey="true"/>
  <Column Name="Code" Member="Code" Storage="mMaterialCode" DbType="Char(4)"/>
  <Column Name="Description" Member="Description" Storage="mDescription" DbType="VarChar(50)"/>
  <Column Name="UnitPrice" Member="UnitPrice" Storage="mUnitPrice" DbType="Decimal(5,2)"/>
</Type>

WinForm Loading

XmlMappingSource mapping = XmlMappingSource.FromUrl("dbmap.xml");
mContext = new DataContext(Properties.Settings.Default.dbConn, mapping);
reloadTable();
+1  A: 

I think the problem you might be getting is that the assembly/class library containing your classes hasn't been loaded into memory at this stage?

Try accessing a class in the library before instantiating the data context to see if that works.

DamienG
Thanks, as soon as I declare an object with the type it works, strange it wouldn't take it from my Table<Material> decleration in the program.
ShaneB
When it comes across the Type name it tries to find the type in the currently loaded assemblies only. That method is scheduled for a rewrite as part of a couple of bugs I'm working on ;-)
DamienG
A: 

I have the same problem. We have a mapping file which maps different assemblies and we get the mapping error since all assemplies are not loaded in memory. What are the different solutions to this problem?

I have read that is is possible to reference a type in a different assembly using the AssemblyQualifiedName of the type. But we did not get it to work. If this is possible can you give an example of what this would look like?

Another solution, I suppose, is to have one mapping file for each assembly and give the datacontext the correct mapping file at runtime.

Michael