外键或虚拟字段

时间:2014-06-17 20:10:08

标签: model-view-controller foreign-key-relationship

我有一个Project表,其中一些字段指向Participant表,但当然所有字段都有不同的含义:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace iMaSys.Models
{
    public class Project
    {
        [Key]
        public int ProjectID { get; set; }

        [Required]
        [StringLength(128)]
        [Display(Name = "Omschrijving")]
        public string Description { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Startdatum")]
        public DateTime StartDate { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        [Display(Name = "Einddatum")]
        public DateTime? EndDate { get; set; }

        public Boolean Active { get; set; }

        [Display(Name = "Mediation Casenummer")]
        public string Mediation_casenr { get; set; }



        public  Participant CaseManager { get; set; }
        public  Participant Supervisor { get; set; }
        public  Participant Party_1 { get; set; }
        public  Participant Party_2 { get; set; }
        public  Participant Client { get; set; }
        public  Participant InterventionManager { get; set; }
        public  Participant Mediator { get; set; }

        public virtual ProjectCategorySub ProjectCategorySubject { get; set; }

        }
    }

Casemanager,Supervisor,Party_1等领域都指向参与者。在Particpant中,我添加了InverseProperties来告诉框架项目和参与者之间的联系:

[InverseProperty("Mediator")]
public List<Project> ProjectMediators { get; set; }

[InverseProperty("Supervisor")]
public List<Project> ProjectSupervisors { get; set; }
etc. etc.

在add-migration和update-database之后,Project中的字段命名为:

CaseManager_ParticipantID等我只需要CaseManagerID指向Participant_ParticipantID。我尝试了几件事,但我似乎不明白如何让它发挥作用。

我坚持这个,有人能指出我正确的方向吗?

最好的问候,Janno Hordijk

1 个答案:

答案 0 :(得分:0)

我自己解决了,但想分享我发现的东西。

参与者:

    [InverseProperty("Mediator")]
    public virtual ICollection<Project> MediatorProjects { get; set; }
    [InverseProperty("Supervisor")]
    public virtual ICollection<Project> SupervisorProjects { get; set; }

在项目:

    [ForeignKey("Mediator")]
    public int? Mediator_ID { get; set; }
    public virtual Participant Mediator { get; set; }

    [ForeignKey("Supervisor")]
    public int? Supervisor_ID { get; set; }
    public virtual Participant Supervisor { get; set; }

现在数据库没问题,我的字段名是Supervisor_ID而不是Supervisor_Participant_ID

最好的问候,Janno