Today I Learned #1

While using EntityFramework in my integration tests (which is a separate topic 😉 ) I discovered quite interesting thing. I guess this may be obvious to some, but I learned Entity “the hard way” jumping into an app with Entity already in place and had to adapt – this was my first app with a database by the way.

So if you add entities to your context I’m used to adding all entities to context, so the code would look like

using (var ctx = new Context())
{
    var first = new FirstEntity { .. };
    var second = new SecondEntity { .. };

    ctx.FirstEntities.Add(first);
    ctx.SecondEntities.Add(second);
    ctx.SaveChanges();
}

But if entities are related, you can safely do this

using (var ctx = new Context())
{
    var first = new FirstEntity { .. };
    var second = new SecondEntity { Relation = first };

    //this will also take care of the first one!
    ctx.SecondEntities.Add(second); 
    ctx.SaveChanges();
}

Or even this!

using (var ctx = new Context())
{
    var second = new SecondEntity { Relation = new FirstEntity{ .. } };

    ctx.SecondEntities.Add(second);
    ctx.SaveChanges();
}

It’s nice and saves some typing! 🙂

End of GetNoticed!

Hello!

GetNoticed competition ended with the start of June. I didn’t make it to the finalists’ list, but I still am proud that I managed to do something. What’s next? Will I abandon writing/coding OSII? Definitely not! I’ve had some busy time with music, so I stepped back a little, but also “cleared my mind” with something new, which I will write about. I still want to finish at least ZMTP part of OSII, I still want to write – end of competition changes only the recurring times of blog posts. I will try to write at least once a week now – not twice as stated in competition rules.

That’s it for now! See you soon! 🙂