Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

LINQ. Who knows how to do this?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shapper - 17 Oct 2007 12:58 GMT
Hello,

I created 3 few tables that have Many to Many relationships:

Posts (PostId PK)
Files (FileId PK)

PostsTags (PostId PK, TagId PK)
FilesTags (FileId PK, TagId PK)

Tags (TagId PK, TagName)

First of all, by dragging the tables to a dbml file the classes are
not ready for such Many to Many relationship:

Posts > PostsTags > Tags
and
Files > FilesTags > Tags

Does anyone knows how to do this?

And the behavior which seems logic to me (but maybe I am wrong) should
be:
(The example for Posts. For files is the same)

- When a tag is added to a Post, a record is added to TagsInPosts.
 If the Tag (given its TagName) does not exist in Tags then it is
created in Tags table.

- When a tag is removed from a post then it is deleted from PostsTags.
 If that tag is no longer associated with a Post or a File through
PostsTags and FilesTags then it is deleted from Tag.

Can I implement this logic in my LINQ classes or should I need to
create triggers, besides the constrains I enumerated, in my database?

I only find simple one-to-one examples and the only many-to-many
example is back to May 2006.

Thanks,
Miguel
Mark Dykun - 17 Oct 2007 20:06 GMT
Miguel,

In your database do you have relationships setup. When you create your LINQ
to SQL classes it uses the relationship data to determine the shape of the
objects. I created the 5 tables that you mentioned;

Post
File
Tag
PostTag
FileTag

I then setup the relationships inside if Enterprise manager. Created a LINQ
to SQL class including all of the tables. and then wrote the following
query;

LinqTestDbDataContext db = new LinqTestDbDataContext();

db.Log = Console.Out;

var values = from file in db.Files

select new { file.FileTag.Tag };

foreach (var value in values)

{

Console.WriteLine(value.Tag.TagID.ToString());

}

with the logging on I inspected the SQL that was created for execution

SELECT [t3].[test], [t3].[TagID], [t3].[TagDescription]

FROM [dbo].[File] AS [t0]

LEFT OUTER JOIN [dbo].[FileTag] AS [t1] ON [t1].[FileTagID] = [t0].[FileID]

LEFT OUTER JOIN (

SELECT 1 AS [test], [t2].[TagID], [t2].[TagDescription]

FROM [dbo].[Tag] AS [t2]

) AS [t3] ON [t3].[TagID] = [t1].[TagID]

As you can see it does work with Many-to-Many relationships. Just make sure
that you have relationships defined in the database.

Mark

> Hello,
>
[quoted text clipped - 37 lines]
> Thanks,
> Miguel
shapper - 18 Oct 2007 13:08 GMT
> Miguel,
>
[quoted text clipped - 90 lines]
> > Thanks,
> > Miguel

Mark,

I am checking everything in my database and I will try again using
LINQ with Many to Many relationships.

Thanks,
Miguel

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.