0

I'm trying to use the database-first approach with the EF 5.0 and it doesn't create the correct model for me.

Here is my data structure:

Forums

Forums

Users

Users

Posts

Posts

Relationships Relationships

The problem

The main problem is with the one-to-one relationships in the Forums and the Posts table. The model doesn't recognize the ParentForumID and the ReplyTo as nullables and therefore creates a one-to-many relationship between the tables (Forum-Forum and Post-Post).

Also, when trying to manually change to One-To-One relationship, I get this error:

Error 113: Multiplicity is not valid in Role 'Forum1' in relationship 'FK_Forums_Forums'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *. ...\Models\Entities\Model1.edmx

The model in Visual Studio (before editing)

The model

I tried using the code-first approach but it produced a poor database result, I then realized that I can configure the behaviour of the database creation but I couldn't find a good resource that explains how to configure it correctly.

I'd rather use the database-first approach since it lets me customize my database but it still generates an incorrect model with many errors.

So my question is:

  1. What is the best approach to do so?

  2. Where can I learn the code first, database first thoroughly?

  3. Why does my visual studio produce such a model?

  4. Why when I try to change the model to fit my needs it gives me the error described before?

  5. Where can I learn about the migration tools? The functions that I can use to configure the database creation?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Imri Barr
  • 1,035
  • 3
  • 12
  • 20
  • I don't understand, shouldn't parent forum to child forum be 1 to many? And parent post to child post as well? What's the point of having a 1 to 1 parent forum to child forum? –  Feb 10 '13 at 01:41

2 Answers2

0

Question 1: It depends.

If you have the code written, or you prefer to write the objects first, then go that route.

If on the other-hand, you prefer doing the DB schema first, then by all-means, do that.

It matters not, so choose what's most comfortable to you.

John
  • 633
  • 4
  • 9
0

Question 2 - 5: You can learn this any number of ways. Some folks prefer buying one of the dozens of very good books on EF with C# Programming that are available.

Others prefer taking a class to boot-strap them on this whole Entity Framework technology. I work for one such company, we have terrific classes. I'm not here to push one training company over another, so I'll make a recommendation that is separate from my employer ~ Plural Sight. They are very good, you can find a link to a code-first EF approach training program here. This is a paid-course.

My approach included just digging around trying different models so I understood how the EF Design Surface works. As a result, I've started preferring the DataBase first modeling approach, same as you. Just hang-in there and keep banging away in the tool, trying different things.

I might be able to help you on a limited basis. I can't tell you why EF created the model it did because I can't know what you did to get this far, now one can. Feel free to contact me thru StackOverflow if you need a little coaching with this.

Good luck!

John
  • 633
  • 4
  • 9