Home

LINQPad6 LINQ to SQL driver - self referential table

Hi, and let me congratulate you for the LINQPad6 preview, looks amazing already.

I was doing some tests on my company's database and I've noticed the following.

One of our tables contains a self referential association. Using ILSpy I can see what LINQ2Sql driver was generating as a class definition. I've stripped out the irrelevant details and it comes down to the following:

public class TheEntityName
{
[Column(IsPrimaryKey = true, DbType = "Int NOT NULL", UpdateCheck = UpdateCheck.Never)]
public int EntityId;

[Column(IsPrimaryKey = true, CanBeNull = false, DbType = "VarChar(50) NOT NULL", UpdateCheck = UpdateCheck.Never)]
public string Key;

private EntityRef _Child;
private EntityRef _TheEntityName;


[Association(Name = "FK_TheEntityName_TheEntityName", Storage = "_TheEntityName", ThisKey = "EntityId,Key", OtherKey = "EntityId,Key", IsForeignKey = true)]
public TheEntityName TheEntityName
{
get { return _TheEntityName.Entity; }
set { _TheEntityName.Entity = value; }
}

[Association(Name = "FK_TheEntityName_TheEntityName", Storage = "_Child", ThisKey = "EntityId,Key", OtherKey = "EntityId,Key", IsForeignKey = false, IsUnique = true)]
public TheEntityName Child
{
get { return _Child.Entity; }
set { _Child.Entity = value; }
}
}
I suppose that the code above is generated by the runtime directly as obviously if you tried to set up something similar in C# you would get a compile time error:
CS0542: member names cannot be the same as their enclosing type.

LINQPad6 is definitely attempting to perform the same as I can see this error when I'm trying to set up a connection.

To be absolutely fair, I'm not sure what the actual reasoning behind the association above currently is and probably I could remove it and that would solve the problem. However in a more general approach and assuming that such association is indeed necessary, is it possible to modify the driver code in order to generate a property name that does not violate CS0542?

Thanks

Comments

  • Thanks for the info. Are you able to compare it with the code that LINQPad 5 generates?
  • Hi Joe

    The code above is the decompiled result from ILSpy from the code that LINQPad 5 generates. Unfortunately I'm not sure if I can check what LINQPad 6 generates as it fails with the error message above. However let me know if I can provide any assistance.

  • Sorry, I misread your message. LINQPad 5 used Reflection.Emit to generate typed data contexts, whereas LINQPad 6 uses C# and Roslyn (because Reflection.Emit doesn't let you save the generated assembly in .NET Core.) That's why the error is now cropping up. I can fix this, but it will mean that some queries written in LINQPad 5 won't work in LINQPad 6 without modification.
  • Can you post the DDL to create that table? (It's easier than trying to infer it out from the generated code.)
  • No worries, I don't think I was clear enough in the first place.

    The following should result in the same issue.


    CREATE TABLE [dbo].[TestTable]
    (
    [Id] [int] NOT NULL,
    [Key] [varchar](50) NOT NULL,
    CONSTRAINT [PK_LoanTypeProperties] PRIMARY KEY CLUSTERED
    (
    [Id] ASC,
    [Key] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[TestTable] WITH CHECK ADD CONSTRAINT [FK_TestTable_To_TestTable] FOREIGN KEY([Id], [Key])
    REFERENCES [dbo].[TestTable] ([Id], [Key])
    GO

    ALTER TABLE [dbo].[TestTable] CHECK CONSTRAINT [FK_TestTable_To_TestTable]
    GO
    Again, I have to say that I have no idea what the foreign key is supposed to be, but it is what it is.
  • This is now fixed in the latest auto-update.
  • Perfect. I tested it and it works. Thanks for the turnaround Joe
Sign In or Register to comment.