1

I have problem with creating relation. What is the best desing in scenerio similar to this. I have one common class "Image" to all other classes which contain images. One image should have only one class. But sometimes it is Class1, other time Class2 etc.

public class Class1
        {
            int Id ;
            List<Image> Images;
        }
        public class Class2
        {
            int Id;
            List<Image> Images;
        }
        public class Class3
        {
            int Id;
            List<Image> Images;
        }
        public class Image
        {
            int Id;
            int IdClass1;
            int IdClass2;
            int IdClass3;

            public Class1 Class1;
            public Class2 Class2;
            public Class3 Class3;
        }
Malv20
  • 159
  • 4
  • 11

1 Answers1

0

I would suggest making the Class1, Class2, Class3, and corresponding identifiers nullable. You would have to enforce constraints at the code level though, instead of relying on the database for those constraints.

Joe Brunscheon
  • 1,949
  • 20
  • 21
  • Yes I can but what if I had 10 more classes it could be overwhalming. I think that it isn't the best approach. I was thinking about giving to all classes GUID identifier and than my image could have only ID, directory and GUID referencing to corresponding object. But I'm not sure. – Malv20 Aug 08 '13 at 20:09
  • Then you should change your design. Try defining a junction table (class really) of some kind that would link a "class" to a bunch of images. Then each class would have a list of these junctions as part of its model. – Joe Brunscheon Aug 08 '13 at 20:17