简体   繁体   English

实体框架代码第一个外键

[英]Entity Framework Code First foreign key

I have problem with creating relation. 我在建立关系方面遇到问题。 What is the best desing in scenerio similar to this. 与此类似,scenerio中最好的设计是什么。 I have one common class "Image" to all other classes which contain images. 对于包含图像的所有其他类,我有一个公共类“ Image”。 One image should have only one class. 一幅图像应该只有一类。 But sometimes it is Class1, other time Class2 etc. 但有时是Class1,其他时间是Class2等。

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;
        }

I would suggest making the Class1, Class2, Class3, and corresponding identifiers nullable. 我建议使Class1,Class2,Class3和相应的标识符为空。 You would have to enforce constraints at the code level though, instead of relying on the database for those constraints. 但是,您将不得不在代码级别强制实施约束,而不是依赖数据库来执行这些约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM