LINQ to SQL - Problem with 1-to-1 association

2019-07-01 19:54发布

问题:

In the L2S designer I have dropped a table and a view. I tried adding an association between the 2 on their primary keys. This should be a one-to-one relationship, so I set the cardinality property of the association as such.

But, when coding I can't access the child property.

Any suggestions?

Edit

I just created a view in sql server that has the fields I want, and dropped that into L2S. Much simpler.

回答1:

In my experience Linq To SQL requires the Primary/Foreign key relationships established in the DB. Otherwise, you'll have to do a join.

Not sure if this applies to your situation, but it may...

Linq To SQL Without Explicit Foreign Key Relationships

UPDATE:

It appears that you can get what you're looking for without establishing the Primary/Foreign key relationships in the database. In the LinqToSQL designer set primary keys on both columns on each table. I also had to turn off Auto-Sync on Parent Table's column that I assigned as a primary key.

Auto-Sync is described here.. Instructs the common language runtime (CLR) to retrieve the value after an insert or update operation.

Link: http://msdn.microsoft.com/en-us/library/bb386971.aspx



回答2:

We had this problem with views. We simply defined the keys in the DBML editor and the property was finally defined.



回答3:

Did you disable (set to false) either the child or parent property?

Did you perhaps mapped the one-to-one relationship the wrong way around (like I did less than an hour ago)?