It is highly recommended to specify the primary key for each entity class. If the primary key values are generated by the database, it also should be specified.
If the primary key values should be provided by the application, it is necessary to define a unique value of table primary key when inserting a new record. It can be autoincremented integer value or some char sequence. LinqConnect offers a wide range of key generation algorithms that allow you to create any keys for your data:
- Guid generator
- Sequence generator
- SequenceHiLo generator
- TableHiLo generator
- Custom generator
Specifying the database name is optional, and this database name will be used only if the connection itself does not specify the database name. If the database name is not specified in the context mapping and in the connection, the model database is assumed to have the same name as the context class.
Attribute Mapping
When using attribute mapping, the primary key members are defined using IsPrimaryKey and IsDbGenerated properties of the Column attribute.
C#csharp | Copy Code |
---|---|
[Table] public class Company { [Column(IsPrimaryKey=true, IsDbGenerated=true)] public int Id { get; set; } // ... } [Table] public class OrderDetail { [Column(IsPrimaryKey=true)] public string OrderID { get; set; } [Column(IsPrimaryKey=true)] public string ProductID { get; set; } // ... } |
Visual Basic | Copy Code |
---|---|
<Table()> _ Public Class Company <Column(IsPrimaryKey:=True, IsDbGenerated:=True)> _ Public Property Id As Integer '... End Class <Table()> _ Public Class OrderDetail <Column(IsPrimaryKey:=True)> _ Public Property OrderID As String <Column(IsPrimaryKey:=True)> _ Public Property ProductID As String '... End Class |
Key generators are set using the corresponding generator attributes.
- GuidGenerator
- SequenceGenerator
- SequenceHiLoGenerator
- TableHiLoGenerator
- CustomGenerator
C#csharp | Copy Code |
---|---|
[Table] public class MyGuidClass { [Column(IsPrimaryKey = true)] [GuidGenerator] public Guid Id { get; set; } // ... } [Table] public class MySequenceClass { [Column(IsPrimaryKey=true)] [SequenceGenerator(Sequence="MY_SEQUENCE")] public Int64 Id { get; set; } // ... } [Table] public class MySequenceHiLoClass { [Column(CanBeNull = false, IsPrimaryKey = true)] [SequenceHiLoGenerator(Sequence = "MY_HILO_SEQUENCE", MaxLo = 1000)] public int Id { get; set; } // ... } [Table] public class MyTableHiLoClass { [Column(IsPrimaryKey = true)] [TableHiLoGenerator(Table = "MyTable1", Column = "MyColumn1", KeyFieldName = "MyKeyFieldName1", KeyFieldValue = "MyKeyFieldValue1", MaxLo = 1000)] public int Id { get; set; } // ... } [Table] public class MyCustomClass { [Column(IsPrimaryKey=true)] [CustomGenerator(ClassName="MyNamespace.MyKeyGenerator", Parameters = new object[] { "MY_VALUE" })] public string Id { get; set; } // ... } |
Visual Basic | Copy Code |
---|---|
<Table()> _ Public Class MyGuidClass <Column(IsPrimaryKey:=True)> _ <GuidGenerator()> _ Public Property Id As Guid '... End Class <Table()> _ Public Class MySequenceClass <Column(IsPrimaryKey:=True)> _ <SequenceGenerator(Sequence:="MY_SEQUENCE")> _ Public Property Id As Int64 '... End Class <Table()> Public Class MySequenceHiLoClass <Column(CanBeNull:=False, IsPrimaryKey:=True)> _ <SequenceHiLoGenerator(Sequence:="MY_HILO_SEQUENCE", MaxLo:=1000)> _ Public Property Id As Integer '... End Class <Table()> Public Class MyTableHiLoClass <Column(IsPrimaryKey:=True)> _ <TableHiLoGenerator(Table:="MyTable1", Column:="MyColumn1", KeyFieldName:="MyKeyFieldName1", KeyFieldValue:="MyKeyFieldValue1", MaxLo:=1000)> _ Public Property Id As Integer '... End Class <Table()> Public Class MyCustomClass <Column(IsPrimaryKey:=True)> _ <CustomGenerator(ClassName:="MyNamespace.MyKeyGenerator", Parameters:=New Object() {"MY_VALUE"})> _ Public Property Id As String '... End Class |
XML Mapping
When using XML mapping, the corresponding IsPrimaryKey and IsDbGenerated attributes of the column tag are used.
<Table> <Type Name="Company"> <Column Member="Id" IsPrimaryKey="true" IsDbGenerated="true" /> ... </Type> </Table> <Table> <Type Name="OrderDetail"> <Column Member="OrderID" IsPrimaryKey="true" /> <Column Member="ProductID" IsPrimaryKey="true" /> ... </Type>
Generators for keys are defined with Generator and GeneratorParameter tags. The Name attribute of the Generator tag specifies the generator kind. GeneratorParameter tags have two attributes - Name and Value. They specify parameters passed to generators.
<Table> <Type Name="MyGuidClass"> <Column Member="Id" IsPrimaryKey="true" /> <Generator Name="Guid" /> </Column> ... </Type> </Table> <Table> <Type Name="MySequenceClass"> <Column Member="Id" IsPrimaryKey="true" /> <Generator Name="Sequence"> <GeneratorParameter Name="Sequence" Value="MY_SEQUENCE" /> </Generator> </Column> ... </Type> </Table> <Table> <Type Name="MySequenceHiLoClass"> <Column Member="Id" IsPrimaryKey="true" /> <Generator Name="SequenceHiLo"> <GeneratorParameter Name="Sequence" Value="MY_HILO_SEQUENCE" /> <GeneratorParameter Name="MaxLo" Value="1000" /> </Generator> </Column> ... </Type> </Table> <Table> <Type Name="MySequenceClass"> <Column Member="Id" IsPrimaryKey="true" /> <Generator Name="TableHiLo"> <GeneratorParameter Name="Table" Value="MyTable1" /> <GeneratorParameter Name="Column" Value="MyColumn1" /> <GeneratorParameter Name="KeyFieldName" Value="MyKeyFieldName1" /> <GeneratorParameter Name="KeyFieldValue" Value="MyKeyFieldValue1" /> <GeneratorParameter Name="MaxLo" Value="1000" /> </Generator> </Column> ... </Type> </Table> <Table> <Type Name="MyCustomClass"> <Column Member="Id" IsPrimaryKey="true" /> <Generator Name="Custom" Class="MyNamespace.MyKeyGenerator"> <GeneratorParameter Name="" Value="MY_VALUE" /> </Generator> </Column> ... </Type> </Table>
Fluent Mapping
FluentMappingBuilder class methods are used for specifying the primary key members.
C#csharp | Copy Code |
---|---|
FluentMappingBuilder builder = new FluentMappingBuilder(null); builder.Entity<Company>() .PrimaryKey(company => company.Id) .DbGenerated(); builder.Entity<OrderDetail>() .PrimaryKey(orderDetail => new { orderDetail.OrderID, orderDetail.ProductID }); |
Visual Basic | Copy Code |
---|---|
Dim builder As New FluentMappingBuilder(Nothing) builder.Entity(Of Company) _ .PrimaryKey(Function(company) company.Id) _ .DbGenerated() builder.Entity(Of OrderDetail) _ .PrimaryKey(Function(orderDetail) New With {orderDetail.OrderID, orderDetail.ProductID}) |
See the Primary Key Configuration Class topic for more information.
Specifying identity generators with fluent mapping is not supported.