I have two tables with the following definitions:
CREATE TABLE [dbo].[Shows] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (1024) NOT NULL,
[Image] NVARCHAR (1024) NULL,
[Description] NTEXT NULL,
[Seasons] INT NOT NULL,
[FacebookId] BIGINT NULL,
[BackgroundImage] NVARCHAR (200) NULL,
[TheTvDbId] BIGINT NULL,
[NetworkId] UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT [PK_Shows] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Shows_ToNetworks] FOREIGN KEY ([NetworkId]) REFERENCES [dbo].[Networks] ([Id])
);
CREATE TABLE [dbo].[TheTvDb]
(
[Id] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL,
[EntityId] UNIQUEIDENTIFIER NOT NULL,
[TheTvDbId] BIGINT NOT NULL,
[LastUpdated] BIGINT NOT NULL DEFAULT 0,
CONSTRAINT [AK_TheTvDb_EntityId] UNIQUE ([EntityId]),
CONSTRAINT [AK_TheTvDb_TheTvDbId] UNIQUE ([TheTvDbId])
)
What I want to do is to create an association property TheTvDbLastUpdated on the Shows table that points to the property LastUpdated of table TheTvDb. The key linking TheTvDb record with Shows record is EntityId. The relationship is one to one. I know how to create association that will link to the whole TheTvDbTable but I do not know how to link only to this one property, which would make it more convenient to access it from the generated entity classes. Thanks.
I use database-first approach, so I’m looking for a way to do it from the designer of the .edmx file.
That’s not possible. Properties that “point” to something are references/navigation properties. You can’t make a scalar/value type a navigation property.
A convenient helper might be a readonly helper property (implemented in partial class file, not EDMX):
Where
TheTvDbwould be the one-to-one navigation property fromShowtoTheTvDb. If you use lazy loading accessing this property would load theTheTvDbentity from the database first if it hasn’t been already loaded yet. If you don’t use lazy loadingTheTvDbcan benulland accessing the property would cause an exception. You might catch this case:Anyway, you need the navigation property for this, and it doesn’t look like a big win if you write
show.TheTvDbLastUpdatedinstead ofshow.TheTvDb.LastUpdated(actually the win is just saving one single dot).