Search code examples
c#foreign-keysentityef-database-first

C# Database first : Referencing own table's column issue


I'm sorry for the hard-to-understand title.

Normally, this is what i am doing :

  • Add a ADO.NET Entity Data Model (foreign keys included)
  • Tables are now auto generated as classes in my project

now i came to this problem, says i have two tables :

CREATE TABLE [VoucherType]
(
    [ID] nvarchar(10) not null Constraint [PK_VoucherType] Primary Key Clustered , 
    [VNName] nvarchar(50) default '' not null, 
    [ENName] nvarchar(50) default '' not null, 
    [Rowver] [TIMESTAMP] not null
)

CREATE TABLE dbo.[VoucherHeader]
(   
    [ID] INT IDENTITY(1,1) Constraint [PK_VoucherHeader] Primary Key Clustered , -- PRIMARY 
    [DateCreated] datetime not null,
    [Description] nvarchar (512) default '' not NULL ,      
    [VoucherTypeID] nvarchar(10) null
        Constraint [FK_VoucherHeader_VoucherTypeID] Foreign Key (VoucherTypeID) References dbo.VoucherType(ID)
)

Now, in my code, i added a partial class VoucherHeader, my goal is to add a new Property called VoucherTypeName, which comes from VoucherType table :

public partial class VoucherHeader
{
    [DataMember]
    public string VoucherTypeName
    {
        set { }
        get
        {
            if (VoucherType != null) return VoucherType.ENName;
            else return "";
        }
    }
}

Well, now i have the VourcherType.ENName in VourcherHeader class

But what if i added a new column in VoucherHeader table, which will reference its own column :

alter table voucherheader add
    [RelatedID] [int] null 
            Constraint [FK_VoucherHeader_RelatedID] Foreign Key (RelatedID) References dbo.VoucherHeader(ID)

This doesn't work :

public partial class VoucherHeader
{
    [DataMember]
    public string RelatedDescription
    {
        set { }
        get
        {
            if (VoucherHeader != null) return VoucherHeader.Description;
            else return "";
        }
    }
}

Any help is greatly appreciated !


Solution

  • If you use the database-first approach, you just have to update your model from the database after altering the VoucherHeader table.

    This will likely cause to two additional navigation properties to appear: VoucherHeader1 and VoucherHeader2.

    The VoucherHeader1 is for a collection of VoucheHeaders because this particular entity can be the target of many other VoucherHeader entities.

    The VoucherHeader2 will be the related entity of your RelatedID reference. Therefore in your code you will need to reference this entity's description:

    public partial class VoucherHeader
    {
        [DataMember]
        public string RelatedDescription
        {
            set { }
            get
            {
                if (VoucherHeader2 != null) return VoucherHeader2.Description;
                else return "";
            }
        }
    }
    

    You can check the correct property names in your generated entity class (VoucherHeader.cs under your model.tt).