开发者

Why does Entity Framework sometimes create relationships automatically and sometimes doesnt?

I have a simple schema similar to this:

Customer

CustomerId, FirstName, LastName, CreateDt

CustomerFeedback

CustomerFeedbackId, CustomerId, Feedback

There is a foreign key relationship between the two.

  • I create a LINQ to SQL model from this, and a relationship line is drawn between the two tables.
  • I create an Entity Framework model from this, and no relationship is drawn between the tables.

I've created other schemas and the relationships are automatically drawn. I can't see why it's not working here. My schema is more complicated than this, but I'm only trying to add these two tables. There is definitely a foreign key established because when I create a database diagram in enterprise manager and add the tables the relationship is shown.

How does EF decide when to automatically create relationships?

I can manually create it, but i really hate doing that in case I mess it up. I've开发者_如何学Go restarted Visual studio and started from a completely empty model and it's still doing it.


Shot in the dark, but is CustomerFeedback.CustomerId nullable and/or unique (via key)? I've seen the designer act a bit strange in scenarios like that, moreso the nullable FK one than the unique key one.


In the end I just had to do this :

  • Close all management studio designers for tables and diagrams
  • SELECT * INTO Customer2 from Customer
  • Recreate indexes and keys on Customer2
  • Rename Customer > CustomerOld
  • Rename Customer2 > Customer

Now it works :-) No schema change or any other change. I'd previously tried deleting and recreating FKs. The only thing that wasn't respecting them was the EF designer so I don't know what was going on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜