0 Comments

This short blog post serves as remainder how to create one-to-one relationship in Entity Framework code-first with fluent mapping. Every time I have to create a model with this relationship I google for code sample (I tend to forget things like this quite often), and only samples I could find are just too complex and the answer how to do it is hidden somewhere between 50.-60. line:)

Approach described here:

  • I wanted to have one to one relationship where both entities have foreign keys of each other
  • one-to-one described in EF fluent API is basically two one-to-many for both sides of the relationship, which boils that to one-to-one when db is finally created
  • we need to put nullable foreign key on at least one entity.
    That’s important for inserts: EF can’t generate SQL that inserts both entities and sets foreign keys in one db call. So we do the following:
    • first insert one entity, get its primary key (PK),
    • then insert second entity with foreign key from first transaction
    • last transaction is to set first entity foreign key of second entity PK
  • If there’s a simpler or more “correct” solution, please let me know! This is the first approach that worked fine, but I hope there’s an even easier way without one-to-many trick

Here’s working solution in Entity Framework 6.1.2 (probably works with v5 also):

Sample application is available on Github!

0 Comments

After many many years of using good old and reliable Blogengine.net it was time for a change. From time to time I tried Wordpress. After installing it locally (which is so easy now with Web Platform Installer), I would end up wasting several hours searching appropriate theme on various theme sites. Even paid one… But it was just too difficult, I need something simpler, smaller. Static file generators looked interesting, but to be honest, maybe that’s little bit too simple, I have absolutely no experience with them, and probably couldn’t sleep knowing that there’s no even simplest Db behind! Its just too much like “Save as HTML” in Word, and I’m like web developer. People would not take me seriously.

Few months ago I found this blog engine, called just Miniblog, from one Microsoft PM, Mads Kristensen, very famous guy, and very productive guy I must say. He is a developer of Web Essentials, an essential  Visual Studio plugin that every web developer must have. The whole blog engine has maybe 100-150 lines of code in total, and that’s what I like the most – it’s so easy to work with, and if you  have to change, add or remove something, everything is so simple, written inside several Razor .cshtml and .cs files, so easy to change!

For those hundred lines of code, list of features looks quite impressive: https://github.com/madskristensen/miniblog. Its very fast (100/100 on Google Page Speed!), can read Blogengine.net XML files (I just copied those, and that was whole export/import procedure to transfer files from old blog engine), and it plays nicely with Live Writer, the ultimate tool for writing blog posts.  Just don’t know what to ask more, no matter how hard I trySmiješak s namigivanjem

Even if you don’t need a blog engine, I highly recommend reading its source code. It’s a good example of simple approach for a simple problem, with a quality implementation.