EF helps you to survive with horrible DB :-)

I had one question yesterday on the worst DataBase I have ever seen.

Imagine two tables:

  • T1 with three columns:
    • Id (PK)
    • FK1
    • FK2
  • T2 with three columns:
    • Id (PK)
    • FK1
    • FK2

And what they wanted to do is to add a relationship between T1 and T2 with T1FK1 = T2FK1 AND T1FK2 = T2FK2 using Entity Framework.

I first thought that it’s a very strange way to do a relationship many to many but in fact, it’s worst! The relationship they wanted is one to many from T1 to T2! (It should be probably too easy to add a T1Id column in T2 table. No comment).

Likely, Entity Framework is the friend of the developer and it is possible to do this scenario. How?

This is what I did:

I added an ssdl view:

<EntitySet Name=T1T2RelationShip EntityType=TestLaFourmiRougeModel1.Store.T1T2RelationShipEntityType>

    <DefiningQuery>

        SELECT T1.Id AS T1ID, T2.ID AS T2ID

        FROM T1 INNER JOIN T2 ON T1.FK1 = T2.FK1 AND T1.FK2 = T2.FK2

    </DefiningQuery>

</EntitySet>


<EntityType Name=T1T2RelationShipEntityType>


    <Key>

        <PropertyRef Name=T2ID />

    </Key>

    <Property Name=T1ID Type=int />

    <Property Name=T2ID Type=int Nullable=false />

</EntityType>

EntityType Name=T1T2RelationShipEntityType>

Then, I added a relationship 0..1 -> many between T1 and T2 and I mapped it on my SSDL View.


That’s all, folks! [:)] Entity Framework is so good! [:D]


However, in this case, I think they really should change their DB (and probably also their DBA)…

This entry was posted in 7671, 7674, 7675. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>