duckkiller53
Member
- Joined
- May 11, 2009
- Messages
- 5
- Programming Experience
- 3-5
Linq to SQL using datacontext and a map file.
Could someone help me with the following question. I have been reading "Linq in Action" by Fabrice Marquerie, in particular Linq to SQL and mapping objects via an xml map file. The question is this, is there a way to use the map files without explicitly using a Namespace ( ie: a prebuilt .dll that is referenced within a project ) for the <Type> attribute to see my object class.
for example: I have a small Lia.mdf data base with an 'Authors' table. The project creates a map file and then uses that map to instantiate a datacontext. What I had hoped to do was just add the objects to my project, ie: an Author.vb class etc, and then have everything work. The problem I experience was that the project class was obviously recognized when instantiating the class etc but the reference via the map file could not be established for the type.
ie: <Table Name="Author" Member="Author">
<Type Name="Author">
The only way I could get this to work ( and from what I read on the net ) was to create a class library project (named 'Sample' with the object, build it and then reference the Sample.dll in my Linq project.
ie: <Table Name="Author" Member="Author">
<Type Name="Sample.Author">
The above works but is there a way to do this and just use normal object classes within your project. I really want to understand this because I feel the power of being able to have normal objects within a project and then use both Linq to SQL and possibly a normal datalayer would be extremely powerful.
Note: The author of "Linq in Action" did it the same way I did. He used SQLMetal to create the map but he compiled a .dll and then referenced it in his project.
Any help would be greatly appreciated. ( I have referenced the code I used below )
Thanks You
***********Sample**************
Public Sub XmlMapping()
Dim map As XmlMappingSource = _ XmlMappingSource.FromXml(System.IO.File.ReadAllText("AuthorMap.xml"))
Dim dataContext As New DataContext(connString, map)
Dim authors = dataContext.GetTable(Of Sample.Author)()
'Do something with the collection ...
Sub Main()
XmlMapping)
Console.ReadLine()
End Sub
*************Map File******************
<?xml version="1.0" encoding="utf-8" ?>
<Database Name="Lia" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
<Table Name="Author" Member="Author">
<Type Name="Sample.Author">
<Column Name="ID" Member="ID" Storage="_ID" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" UpdateCheck="Never" />
<Column Name="LastName" Member="LastName" Storage="_LastName" DbType="VarChar(50) NOT NULL" CanBeNull="false" UpdateCheck="Never" />
<Column Name="FirstName" Member="FirstName" Storage="_FirstName" DbType="VarChar(30) NOT NULL" CanBeNull="false" UpdateCheck="Never" />
<Column Name="WebSite" Member="WebSite" Storage="_WebSite" DbType="VarChar(200)" UpdateCheck="Never" />
<Column Name="TimeStamp" Member="TimeStamp" Storage="_TimeStamp" DbType="rowversion NOT NULL" CanBeNull="false" IsDbGenerated="true" IsVersion="true" UpdateCheck="Never" AutoSync="Always" />
<Association Name="FK_BookAuthor_Author" Member="BookAuthor" Storage="_BookAuthor" ThisKey="ID" OtherKey="Author" DeleteRule="NO ACTION" />
</Type>
</Table>
</Database>
*************Object Class***************
Public Class Author
Private _Id As Guid
Public Property ID() As System.Guid
Get
Return _Id
End Get
Set(ByVal value As System.Guid)
_Id = value
End Set
End Property
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
Private _WebSite As String
Public Property WebSite() As String
Get
Return _WebSite
End Get
Set(ByVal value As String)
_WebSite = value
End Set
End Property
Private _TimeStamp As Byte()
Public Property TimeStamp() As Byte()
Get
Return _TimeStamp
End Get
Set(ByVal value As Byte())
_TimeStamp = value
End Set
End Property
End Class
Could someone help me with the following question. I have been reading "Linq in Action" by Fabrice Marquerie, in particular Linq to SQL and mapping objects via an xml map file. The question is this, is there a way to use the map files without explicitly using a Namespace ( ie: a prebuilt .dll that is referenced within a project ) for the <Type> attribute to see my object class.
for example: I have a small Lia.mdf data base with an 'Authors' table. The project creates a map file and then uses that map to instantiate a datacontext. What I had hoped to do was just add the objects to my project, ie: an Author.vb class etc, and then have everything work. The problem I experience was that the project class was obviously recognized when instantiating the class etc but the reference via the map file could not be established for the type.
ie: <Table Name="Author" Member="Author">
<Type Name="Author">
The only way I could get this to work ( and from what I read on the net ) was to create a class library project (named 'Sample' with the object, build it and then reference the Sample.dll in my Linq project.
ie: <Table Name="Author" Member="Author">
<Type Name="Sample.Author">
The above works but is there a way to do this and just use normal object classes within your project. I really want to understand this because I feel the power of being able to have normal objects within a project and then use both Linq to SQL and possibly a normal datalayer would be extremely powerful.
Note: The author of "Linq in Action" did it the same way I did. He used SQLMetal to create the map but he compiled a .dll and then referenced it in his project.
Any help would be greatly appreciated. ( I have referenced the code I used below )
Thanks You
***********Sample**************
Public Sub XmlMapping()
Dim map As XmlMappingSource = _ XmlMappingSource.FromXml(System.IO.File.ReadAllText("AuthorMap.xml"))
Dim dataContext As New DataContext(connString, map)
Dim authors = dataContext.GetTable(Of Sample.Author)()
'Do something with the collection ...
Sub Main()
XmlMapping)
Console.ReadLine()
End Sub
*************Map File******************
<?xml version="1.0" encoding="utf-8" ?>
<Database Name="Lia" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
<Table Name="Author" Member="Author">
<Type Name="Sample.Author">
<Column Name="ID" Member="ID" Storage="_ID" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" UpdateCheck="Never" />
<Column Name="LastName" Member="LastName" Storage="_LastName" DbType="VarChar(50) NOT NULL" CanBeNull="false" UpdateCheck="Never" />
<Column Name="FirstName" Member="FirstName" Storage="_FirstName" DbType="VarChar(30) NOT NULL" CanBeNull="false" UpdateCheck="Never" />
<Column Name="WebSite" Member="WebSite" Storage="_WebSite" DbType="VarChar(200)" UpdateCheck="Never" />
<Column Name="TimeStamp" Member="TimeStamp" Storage="_TimeStamp" DbType="rowversion NOT NULL" CanBeNull="false" IsDbGenerated="true" IsVersion="true" UpdateCheck="Never" AutoSync="Always" />
<Association Name="FK_BookAuthor_Author" Member="BookAuthor" Storage="_BookAuthor" ThisKey="ID" OtherKey="Author" DeleteRule="NO ACTION" />
</Type>
</Table>
</Database>
*************Object Class***************
Public Class Author
Private _Id As Guid
Public Property ID() As System.Guid
Get
Return _Id
End Get
Set(ByVal value As System.Guid)
_Id = value
End Set
End Property
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
Private _WebSite As String
Public Property WebSite() As String
Get
Return _WebSite
End Get
Set(ByVal value As String)
_WebSite = value
End Set
End Property
Private _TimeStamp As Byte()
Public Property TimeStamp() As Byte()
Get
Return _TimeStamp
End Get
Set(ByVal value As Byte())
_TimeStamp = value
End Set
End Property
End Class