I am trying to map an obsolete database to an Entity Framework model. The database is very general, and most of the data is stored in the Object and Event tables. Columns are called things like Date1, Num11, Text4. There are no explicit foreign keys in the database.
Here is a subset of the two tables:
CREATE TABLE [Object] ( [ObjectId] int not null identity(1,1) primary key, [ObjectTypeId] int, [Name] varchar(100) ); CREATE TABLE [Event] ( [EventId] int not null identity(1,1) primary key, [EventTypeId] int, [Subject] text, [Body] text, [Date1] datetime, [Num11] decimal(18,2) );
For some EventTypeID values, the Num11 field refers to Object . I can easily write a join between tables:
SELECT ev.[EventId], ev.[Subject], ev.[Body], ev.[Date1] AS [CreatedDate], p.[ObjectId] AS [PersonId], p.[Name] AS [PersonName] FROM [Event] ev LEFT JOIN [Object] p ON p.ObjectId = ev.Num11 WHERE ev.[EventTypeId] = 7 AND ev.[Date1] > '2013-04-07'
In the Entity Framework constructor, I can create separate objects for each type of object and rename the columns accordingly. Problems arise when I try to create navigation properties between objects, because the column type of the foreign key does not always match the primary key.
Neither SQL Server nor Entity Framework will allow me to create a link to foreign keys between columns.
How to create navigation properties between objects when the data types FK an and PK do not match exactly? Something that allows me to include a related object in a LINQ query and hopefully be able to open it in an OData service.
I cannot make any changes to existing tables in the database, but if necessary, I could add views. Although I would need to save the objects back to the database.