Generate primary keys for vb.net

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
lc4pro
Posts: 51
Joined: Thu 12 Jul 2012 08:16

Generate primary keys for vb.net

Post by lc4pro » Thu 12 Jul 2012 08:22

Hello,

we have the problem that the generated VB.Net classes are read only for the primary key field "ID".

We can change the readonly bit within the ui and then the generated code will work but every time we regenerate the Model from database the ID-field is read only again.

This is very annoying.

Can you please help us.

Thanks in advance!

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Generate primary keys for vb.net

Post by MariiaI » Thu 12 Jul 2012 12:04

In most cases, we don't generate the code for a setter of properties that correspond to the autoincrement fields due to the fact that the key value is generated in the database and assigned directly to the field, upon which the primary key property is built (JIC: it is not completely applicable to MS SQL Server, because it has mode allows you to insert data in such columns). Thus, these properties are "Read Only".
However, if you change it and don't select the "Recreate model" check box in the Update From Database Wizard, your model will not be cleared and rebuilt from the scratch. And, if the properties of the column (ID), which corresponds to the entity key (ID) of your class, have not been changed in the database, the entity key "ID" will remain unchanged in the model.

lc4pro
Posts: 51
Joined: Thu 12 Jul 2012 08:16

Re: Generate primary keys for vb.net

Post by lc4pro » Thu 12 Jul 2012 12:20

Thanks!

but I think I forgot to mention that the generated code isn't even compileable.

I can understand why the setter is not generated but then the prototype of the ID-Property is wrong

Code: Select all

        
''' <summary>
''' There are no comments for Id in the schema.
''' </summary>
<Column(Name := "id", Storage := "_Id", AutoSync := AutoSync.OnInsert, CanBeNull := false, DbType := "INT(10) NOT NULL AUTO_INCREMENT", IsDbGenerated := true, IsPrimaryKey := true)> _
Public Property Id As Integer
    Get
        Return Me._Id
    End Get
End Property
There is no ReadOnly-Attribute in the column definition and VB.net needs other than c#.net the ReadOnly declaration in it's prototypes. Otherwise the setters are strictly required and vb.net woun't compile.

Because of our starting rollout of linqConnector to all our projects we would have to correct the readonly bit for all tables on all our projects by hand.

Nothing I'm looking forward to at 200+ tables :P

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Generate primary keys for vb.net

Post by MariiaI » Fri 13 Jul 2012 07:30

Thank you for the report. We will change the behaviour of the code generation for autoincrement properties in VB.Net. This changed behaviour will be available in the next build of LinqConnect. We will post here when it is available.
As a workaround, you could try performing the following:
- open used template in the Model Explorer (to make the template available for editing, copy it to the Model Folder - 'Copy to Model Folder' by right-click on the template in the Model Explorer);
- find the line in the Method GenerateProperty()

Code: Select all

<#= propAccess #><#= propInheritanceModifier #> Property <#= propName #> As <#= propType #>
you should replace it with

Code: Select all

<#= propAccess #><#= propInheritanceModifier #><#= Iif(prop.ReadOnly, " ReadOnly", "") #> Property <#= propName #> As <#= propType #>
- save changes and re-generate the code.

Please notify us about the results.

lc4pro
Posts: 51
Joined: Thu 12 Jul 2012 08:16

Re: Generate primary keys for vb.net

Post by lc4pro » Fri 13 Jul 2012 08:34

Thanks for your Help!

The workaround seems to work in a little test. So lets deploy and see if it works :D

Thanks again!

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Generate primary keys for vb.net

Post by MariiaI » Thu 19 Jul 2012 07:30

New build of LinqConnect 4.0.45 is available for download now!
It can be downloaded from http://www.devart.com/linqconnect/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=31&t=24531

Post Reply