none
LINQ to SQL and relation one to one. Bug ? RRS feed

  • Question

  • Hello,

    I have a table Versions:

    - Id uniqueidentifier (PK)

    - Name nvarchar(30) (NOT NULL)

    - OldVersion uniqueidentifier (allow NULL)

     

    I have a relation between OldVersion and Id

     

    With Linq To SQL designer, I want to have a relation one to one. When I do this on property grid in designer, the code generated by designer does not compile. Indeed, it is:

     

    Code Block
    [Association(Name="Version_Version", Storage="_Versions", ThisKey="Version1", OtherKey="OldVersion", IsUnique=true, IsForeignKey=false)]
    public Version Versions
    {
     get
     {
      return this._Versions.Entity;
     }
     set
     {
      Version previousValue = this._Versions.Entity;
      if (((previousValue != value)
         || (this._Versions.HasLoadedOrAssignedValue == false)))
      {
       this.SendPropertyChanging();
       if ((previousValue != null))
       {
        this._Versions.Entity = null;
        previousValue.Version2 = null;
       }
       this._Versions.Entity = value;
       if ((value != null))
       {
        value.Version2 = this;
        this._Version1 = value.OldVersion;
       }
       else
       {
        this._Version1 = default(System.Guid);
       }
       this.SendPropertyChanged("Versions");
      }
     }
    }

     

     

    The compilation error is:

    Cannot implicitly convert type 'System.Guid?' to 'System.Guid'. An explicit conversion exists (are you missing a cast?).

     



     

    Friday, November 23, 2007 9:50 PM

Answers

  • Why don't you consider using Timestamp column instead of GUID ? Then Linq to SQL will manage the version automatically for you.

    Monday, May 26, 2008 10:19 PM

All replies

  • Hi Matthieu,

    I moved your question from the ADO.NET Entity Framework forum to the LINQ to SQL forum a few days ago, in order to give it new chances of being answered. Please, let us now if you found the solution by yourself.

    Thanks,
    Diego

    Monday, May 26, 2008 3:26 PM
  • H Diego,

    Thanks.

    It's strange that I use the wrong forum. Sorry for that.

    Matthieu

     

    Monday, May 26, 2008 3:30 PM
  • Actually, it wasn't the wrong forum. At the time you posted your question, the current LINQ to SQL forum did not exist Smile

    Monday, May 26, 2008 3:36 PM
  • if i get you correctly, you have a relation between OldVersion and Id (which id) is it on the same table or different one, but either ways, the error seems to be from having a nullable column (OldVersion) which is referencing a non nullable column, so the error says that you cant convert the nullable type System.Guid to a non nullable type System.Guid, so you have to remove the allow null constraint, i would prefer to set it to something over than null like another flag to indicate whether it has an old version or not.

    Monday, May 26, 2008 7:46 PM
  • Why don't you consider using Timestamp column instead of GUID ? Then Linq to SQL will manage the version automatically for you.

    Monday, May 26, 2008 10:19 PM