views:

35

answers:

2

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 restarted Visual studio and started from a completely empty model and it's still doing it.

A: 

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.

ray2k
Pretty sure its not nullable, definitely not unique. Got frustrated and moved to the gym. Wouldn't the fact that it let me manually create the relationship suggest everything is ok?
Simon_Weaver
CustomerId is not nullable in either table, and the identity column for 'Customer'. And like i said LINQ's designer DOES create a relationship, but entity framework doesnt...
Simon_Weaver
Hard to say without seeing your metadata and t-sql. You mentioned enterprise manager which is a sql 2000 tool, are you using Sql Server 2000? If so, it would be interesting to see if you get the same experience using something like the express versions of 2005 or 2008.
ray2k
@ray2k - faux pas - i'm on 2008. i'm giving up on this for now. something bizarre is happening and i need to move on. (see comment to craig above)
Simon_Weaver
A: 

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.

Simon_Weaver