【问题标题】:C# Entity Framework - Multiple relationshipsC# 实体框架 - 多重关系
【发布时间】:2017-05-20 12:37:07
【问题描述】:

我不确定这是否可能,但我想我会问...

我有两个数据库表。一个是从 Active Directory 中提取的用户列表。第二个表是计划转发的列表。我想建立的关系是......

public class AdObject
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string ObjectType { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName { get; set; }
    public string DistinguishedName { get; set; }
    public string PrimaryEmail { get; set; }
    public string Alias { get; set; }
    public string SamAccountName { get; set; }
    public string PrimaryDisplay { get; set; }
    public string CanonicalName { get; set; }
    public string OU { get; set; }
    public string CoreGroup { get; set; }
    public string ForwardedTo { get; set; }
    public bool? IsDisabled { get; set; }
    public bool? IsForwarded { get; set; }
    public bool? DeliverAndRedirect { get; set; }
    public bool? DisableForwardAtLogon { get; set; }
    public DateTime? DisableAtLogonAfter { get; set; }
    public string Notify { get; set; }
    public DateTime? LastLogon { get; set; }
    public DateTime? LastApiLogon { get; set; }
    public DateTime? LastCheck { get; set; }

    // This isn't required. But if possible I would like this to be
    // a relationship to another AdObject whos "PrimaryEmail" matches
    // the "ForwardedTo" column of this AdObject. There will not always
    // be a match though, so not too important just wondering if its possible.
    public AdObject ForwardedToObject { get; set; }

    // This would be a list of forwards where the "ForwardFrom"
    // column matches the "PrimaryEmail" of this AdObject. 
    public ICollection<Forward> ScheduledForwards { get; set; }
        = new List<Forward>();


   // FYI... Technically ID,SamAccountName,PrimaryEmail,DistinguishedName, 
   // and CanonicalName are all unique. They could all be keys. 

}

public class Forward
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string ForwardFrom { get; set; }
    public string ForwardTo { get; set; }
    public bool? DeliverAndRedirect { get; set; }
    public DateTime? StartTime { get; set; }
    public DateTime? StopTime { get; set; }
    public string Recurrence { get; set; }
    public bool? DisableForwardAtLogon { get; set; }
    public DateTime? DisableAtLogonAfter { get; set; }
    public string Notify { get; set; }

    public string StartJobId { get; set; }
    public string StopJobId { get; set; }
    public string StartJobStatus { get; set; }
    public string StopJobStatus { get; set; }
    public DateTime? StartJobCompleted { get; set; }
    public DateTime? StopJobCompleted { get; set; }
    public DateTime? StartJobCreated { get; set; }
    public DateTime? StopJobCreated { get; set; }
    public string StartReason { get; set; }
    public string StopReason { get; set; }

    // This would be the AdObject whos "PrimaryEmail" matches the
    // "ForwardTo" column. 
    public AdObject ForwardToObject { get; set; }

    // This would be the AdObject whos "PrimaryEmail" matches the
    // "ForwardFrom" column. 
    public AdObject ForwardFromObject { get; set; }
}

【问题讨论】:

  • 简单的答案是肯定的。在语法方面,没有什么能阻止您以这种方式设计模型。真正的症结在于你如何实现这个模型的使用,例如问问自己你将如何构建这个模型?
  • 你可能想看看层次设计模式。

标签: c# entity-framework relationships


【解决方案1】:

我想我明白了。我在理解关系背后的逻辑时度过了一段地狱般的时光。我消除了一些误解,在浏览了每个 YouTube 视频、PluralSight 课程和 Udemy 课程后,我发现它终于开始点击了。我通常不会在自学这些东西时遇到很多麻烦,但是我的误解将我引向了错误的方向。最后,我只需要指定键和两个关系(其余的由约定完成)。

public class AdObject
{
    [Key, Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string ObjectType { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName { get; set; }
    public string DistinguishedName { get; set; }
    [Key, Column(Order = 1)]
    public string PrimaryEmail { get; set; }
    public string Alias { get; set; }
    public string SamAccountName { get; set; }
    public string PrimaryDisplay { get; set; }
    public string CanonicalName { get; set; }
    public string OU { get; set; }
    public string CoreGroup { get; set; }
    public bool? IsDisabled { get; set; }
    public bool? IsForwarded { get; set; }
    public bool? DeliverAndRedirect { get; set; }
    public bool? DisableForwardAtLogon { get; set; }
    public DateTime? DisableAtLogonAfter { get; set; }
    public string Notify { get; set; }
    public DateTime? LastLogon { get; set; }
    public DateTime? LastApiLogon { get; set; }
    public DateTime? LastCheck { get; set; }

    public AdObject ForwardedToObject { get; set; }

    public ICollection<Forward> ForwardRecipientSchedule  { get; set; }
        = new List<Forward>();

    public ICollection<Forward> ForwardSchedule { get; set; }
        = new List<Forward>();
}

public class Forward
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public AdObject ForwardFromObject { get; set; }
    public AdObject ForwardToObject { get; set; }
    public bool? DeliverAndRedirect { get; set; }
    public DateTime? StartTime { get; set; }
    public DateTime? StopTime { get; set; }
    public string Recurrence { get; set; }
    public bool? DisableForwardAtLogon { get; set; }
    public DateTime? DisableAtLogonAfter { get; set; }
    public string Notify { get; set; }
    public string StartJobId { get; set; }
    public string StopJobId { get; set; }
    public string StartJobStatus { get; set; }
    public string StopJobStatus { get; set; }
    public DateTime? StartJobCompleted { get; set; }
    public DateTime? StopJobCompleted { get; set; }
    public DateTime? StartJobCreated { get; set; }
    public DateTime? StopJobCreated { get; set; }
    public string StartReason { get; set; }
    public string StopReason { get; set; }


}

public class EntityContext : DbContext
{
    public EntityContext() : base("name=EnityContext"){

    }


    public DbSet<AdObject> AdObjects { get; set; }
    public DbSet<Forward> Forwards { get; set; }

    //protected override void OnConfiguring(DbContext)

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<AdObject>()
            .HasMany(f => f.ForwardSchedule)
            .WithRequired(f => f.ForwardFromObject)
            .WillCascadeOnDelete(false);


        modelBuilder.Entity<AdObject>()
            .HasMany(f => f.ForwardRecipientSchedule)
            .WithRequired(f => f.ForwardToObject)
            .WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);
    }


}

【讨论】:

    猜你喜欢
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多