复杂Ef核心映射/关系

时间:2019-06-13 10:49:20

标签: c# entity-framework entity-framework-core

一个形容词应具有一个类别(一对一), 目标包含操作,并且每个操作都应具有一个操作类别(一对一)和许多资源类别(一对多)。 我不知道如何配置映射。另外我不确定我的设计是否正确,如何正确处理这种情况?

摘要:

我需要3个关系。

1)在Objective和Objective的ObjectiveCategoryId属性上类别之间一对一。

2)ObjectiveAction和ObjectiveAction的ActionCategoryId属性上的类别一对一。

3)类别的ObjectiveId属性上的ObjectiveAction和类别之间一对多。

enter image description here

    public class Category {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Objective {
        private ICollection<DevelopmentObjectiveAction> _actions;

        public int Id { get; set; }
        public string Name { get; set; }

        public int ObjectiveCategoryId { get; set; }
        public Category ObjectiveCategory { get; set; }

        public ICollection<ObjectiveAction> Actions {
            get => _actions ?? (_actions = new List<ObjectiveAction> ());
            set => _actions = value;
        }
    }

    public class ObjectiveAction {
        private ICollection<Category> _resourceCategories;

        public int Id { get; set; }
        public string Name { get; set; }

        public long ObjectiveId { get; set; }
        public long Objective { get; set; }

        public int ActionCategoryId { get; set; }
        public Category ActionCategory { get; set; }

        public ICollection<Category> ResourceCategories {
            get => _resourceCategories ?? (_resourceCategories = new List<Category> ());
            set => _resourceCategories = value;
        }
    }

0 个答案:

没有答案