开发者

Features in C# for SQLCE foreign keys

I know how to do SQL query to add foreign keys, connect to DataBase and fetch data in C# and such. But after long hours searching on Google, I cannot find any features (or classes, methods, API etc.) in C# that has anything to do with Foreign Key.

Back when I was in Rails, if two tables has relationship I can easily access a child t开发者_运维问答able through the parent table by Teacher[0].Classes.Last . But these methods seems to be missing in C#? Am I right?

I'm working with C#, SQLCE and WPF.

I have two tables that has one-to-one relationship. But all the data including the reference key are inserted manually (e.g. this row, insert a "1", that row insert a "5" etc.) and to find a rows in the child table corresponding to a parent table's ID, I just have to do an if statement in C#.

So basically a Foreign Key to me is just another int column. I don't get what the reference actually does or is it just a naming convention? Just so reader of the code sees a reference and recongnize there is a relation, but the foreign key doesn't actually do anything substantially?


Back when I was in Rails, if two tables has relationship I can easily access a child table through the parent table by Teacher[0].Classes.Last, But these methods seems to be missing in C#?

You can do something similar with Entity Framework and many other ORMs out there.

So basically a Foreign Key to me is just another int column. ... Just so reader of the code sees a reference and recongnize there is a relation, but the foreign key doesn't actually do anything substantially?

Correct - the value of the foriegn key loses significance once you leave the database. Instead what is normal is to have nested objects. For example, you can have a Customer object which can contain an Address object. The Address object may also carry around its foriegn key value, but generally you wouldn't use it in the C# code (unless you were doing something like a LINQ query with it) - you would use the foriegn key value once you got back to the database.


A foreign key represents a relationship between objects in your Domain model. It enforces integrity.

(In some databases, foreign keys can also speed up queries because the query optimiser is able to make use of this information)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜