Xml mapping. How to map two or more types to one table?

Xml mapping. How to map two or more types to one table?

Postby Albert » Wed 11 Jun 2014 10:23

Hi,
I have two entities:
Code: Select all
public class ObjType
    {
        long _No;
        public long No { get { return _No; } }

        string _Name;
        public string Name { get { return _Name; } }

    }
    public class ObjType2 :ObjType
    {
    }

and folowing xml mapping:
Code: Select all
<Database xmlns="http://schemas.devart.com/linqconnect/mapping" Name=""
          Provider="Devart.Data.Oracle.Linq.Provider.OracleDataProvider, Devart.Data.Oracle.Linq, Version=4.4.509.0, Culture=neutral, PublicKeyToken=09AF7300EEC23701" >

  <Table Name="OBJ$TYPES">
    <Type Name="ObjType">
      <Column Member="No" Name="NO" Storage="_No" IsPrimaryKey="true" />
      <Column Member="Name" Name="NAME" Storage="_Name" />
    </Type>
  </Table>
  <Table Name="OBJ$TYPES">
    <Type Name="ObjType2">
      <Column Member="No" Name="NO" Storage="_No" IsPrimaryKey="true" />
      <Column Member="Name" Name="NAME" Storage="_Name" />
    </Type>
  </Table>
</Database>

Then I try to run this code:
Code: Select all
            DataContext dc = new Context(conn, Devart.Data.Linq.Mapping.XmlMappingSource.FromXml(xml));

            Console.WriteLine(dc.GetTable<ObjType>().First().Name);// this works
            Console.WriteLine(dc.GetTable<ObjType2>().First().Name);// this fails

Last line of code fails with "An unhandled exception of type 'System.InvalidOperationException' occurred in Devart.Data.Linq.dll. Additional information: Sequence contains more then one element" and the following StackTrace:
Code: Select all
   at Devart.Data.Linq.DataProvider.a(c1 A_0, IDbConnection A_1, IDataReader A_2)
   at Devart.Data.Linq.DataProvider.a(c1 A_0, Object[] A_1)
   at Devart.Data.Linq.DataProvider.c(Expression A_0)
   at Devart.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.First[TSource](IQueryable`1 source)
   at TestDevart2.Program.Main(String[] args) in c:\Users\KadyrovA\Documents\Visual Studio 2013\Projects\TestDevart2\TestDevart2\Program.cs:line 32
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

Is there possibility to map two types to one table? If yes, what am i doing wrong?
Thanks!
Albert
 
Posts: 20
Joined: Fri 06 Dec 2013 06:38

Re: Xml mapping. How to map two or more types to one table?

Postby MariiaI » Thu 12 Jun 2014 12:23

Is there possibility to map two types to one table?
No, there is no way.

Please take a look at these topics about the inheritance in LinqConnect:
http://www.devart.com/linqconnect/docs/?Inheritance.html
http://www.devart.com/linqconnect/docs/?InheritanceMapping.html
Probably, in your case the TPH inheritance would be the most suitable solution.
If you have any further questions regarding this, feel free to contact us.

JIC: there is a such possibility in Entity Framework:
http://blogs.msdn.com/b/adonet/archive/2008/12/05/table-splitting-mapping-multiple-entity-types-to-the-same-table.aspx
Table Splitting can be easily (no manual editing of edml) made in Entity Developer: drag&drop particular (unnecessary or with big data) properties from table on the diagram surface and choose Table Splitting in the dialog.
MariiaI
Devart Team
 
Posts: 1472
Joined: Mon 13 Feb 2012 08:17


Return to LinqConnect (LINQ to SQL support)