LINQ to SQL Basic Question - Table Relationship

I play with an image gallery in ASP.NET MVC and try to make out LINQ to SQL at the same time. I have 3 tables for storing data in an image gallery with many relationships through a link table. It:

Gallery
(Id, Name, Description)

Image
(Id, Title, Description, FileName)

GalleryImage
(GalleryId, ImageId)

GalleryImage has an FK relationship configured for two other tables.

I want to be able to upload my galleries and display the images associated with each of them, as well as be able to display one image and list the galleries with which it is associated. Since I'm new to Linq for SQL, I don't know how to do this. Can someone guide me please?

+5
source share
4 answers

Step 1: create associations between tables. You will know that this is done correctly when

  • Gallery class has GalleryImages property
  • the GalleryImage class has the Gallery and image property (note, single)
  • The Image class has the GalleryImages property.

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

Step 2: To get a list of galleries:

using (CustomDataContext myDC = new CustomDataContext)
{
  List<Gallery> result = myDC.Galleries.ToList();
}

Step 3: then the user clicks on the gallery and you want its images:

using (CustomDataContext myDC = new CustomDataContext)
{
  List<Image> result = myDC.Galleries
    .Where(g => g.Id = selectedGallery.Id);
    .SelectMany(g => g.GalleryImages)
    .Select(gi => gi.Image)
    .ToList()
}

Step 4: Then the user clicks on the image and you want his gallery:

using (CustomDataContext myDC = new CustomDataContext)
{
  List<Gallery> result = myDC.Images
    .Where(i => i.Id = selectedImage.Id);
    .SelectMany(i => i.GalleryImages)
    .Select(gi => gi.Galleries)
    .ToList()
}

If you just want to download the entire database, do the following:

using (CustomDataContext myDC = new CustomDataContext)
{
  DataLoadOptions dlo = new DataLoadOptions();
  dlo.LoadWith<Gallery>(g => g.GalleryImages);
  dlo.LoadWith<GalleryImage>(gi => gi.Image);
  myDC.LoadOptions = dlo;

  List<Gallery> result = myDC.Galleries.ToList();
}

After that, the entire graphic will be downloaded and connected for your use.

+1
Gallery.GalleryImages<GalleryImage>

GalleryImage , GalleryImage :

GalleryImage.Image

, DBML?

+1

DataContext.

# dbml EntitySet, Association - Gallary Image , GallaryImage - 2. dbml , - -

, , , -

DataConext dc = new GalleryDataConext();
foreach (Gallery g in dc.Gallerys)
{
  Console.Writeline("gallery id " + g.Id.ToString());
  foreach(GalleryImage gi in g.GalleryImages)
   {
      Console.Writeline("galleryimage id " + gi.Id.ToString());
      foreach(Image i in gi)
       {
         Console.Writeline("image id " + i.Id.ToString());
       }
   }

:

int GalID = 1;
GalleryDataConext dc = new GalleryDataConext()
var pics = from g in dc.Gallary
           join gi in dc.GallaryImages on g.Id equals gi.GallaryId
           join i in dc.Images on gi.ImageId equals i.Id
           where g.Id = GalID
           select i;

pic, -

int PicID = 1;
var gals = from g in dc.Gallary
           join gi in dc.GallaryImages on g.Id equals gi.GallaryId
           join i in dc.Images on gi.ImageId equals i.Id
           where i.Id = PicID
           select g;

IQueryable<Gallary> sql-, .

+1

You need to create relationships between tables. if you are sure that you have created relationships, then there may be child or parent properties of classes that can be set on the Internet, they must be publicly available.

-1
source

All Articles