C#实体框架-多重关系
我不确定这是否可能,但我想我会问 我有两个数据库表。一个是从Active Directory中提取的用户列表。第二个表是计划转发的列表。我想建立的关系是C#实体框架-多重关系,c#,entity-framework,relationships,C#,Entity Framework,Relationships,我不确定这是否可能,但我想我会问 我有两个数据库表。一个是从Active Directory中提取的用户列表。第二个表是计划转发的列表。我想建立的关系是 public class AdObject { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string ObjectType { get; set; } pu
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; }
}
公共类AdObject
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
公共字符串对象类型{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串DisplayName{get;set;}
公共字符串区分名称{get;set;}
公共字符串PrimaryEmail{get;set;}
公共字符串别名{get;set;}
公共字符串SamAccountName{get;set;}
公共字符串PrimaryDisplay{get;set;}
公共字符串CanonicalName{get;set;}
公共字符串OU{get;set;}
公共字符串核心组{get;set;}
公共字符串转发到{get;set;}
公共布尔?已禁用{get;set;}
公共bool?IsForwarded{get;set;}
公共bool?deliverandDirect{get;set;}
公共bool?DisableForwardAtLogon{get;set;}
公共日期时间?在{get;set;}
公共字符串通知{get;set;}
公共日期时间?LastLogon{get;set;}
公共日期时间?LastApiLogon{get;set;}
公共日期时间?LastCheck{get;set;}
//这不是必需的。但如果可能的话,我希望是这样
//与另一个“PrimaryEmail”匹配的对象的关系
//此AdObject的“ForwardedTo”列。不总是
//不过,这是一场比赛,所以不太重要,只是想知道这是否可能。
public AdObject ForwardedToObject{get;set;}
//这将是一个转发列表,其中“ForwardFrom”
//列与此AdObject的“PrimaryEmail”匹配。
公共ICollection ScheduledForwards{get;set;}
=新列表();
//仅供参考…技术ID、SamAccountName、PrimaryEmail、DifferentizedName、,
//CanonicalName和CanonicalName都是唯一的。它们都可能是键。
}
公开课前进
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
从{get;set;}转发的公共字符串
公共字符串转发到{get;set;}
公共bool?deliverandDirect{get;set;}
公共日期时间?开始时间{get;set;}
公共日期时间?停止时间{get;set;}
公共字符串重复{get;set;}
公共bool?DisableForwardAtLogon{get;set;}
公共日期时间?在{get;set;}
公共字符串通知{get;set;}
公共字符串StartJobId{get;set;}
公共字符串StopJobId{get;set;}
公共字符串StartJobStatus{get;set;}
公共字符串StopJobStatus{get;set;}
公共日期时间?StartJobCompleted{get;set;}
公共日期时间?StopJobCompleted{get;set;}
公共日期时间?StartJobCreated{get;set;}
公共日期时间?StopJobCreated{get;set;}
公共字符串StartReason{get;set;}
公共字符串StopReason{get;set;}
//这将是“PrimaryEmail”与
//“ForwardTo”列。
公共AdObject ForwardToObject{get;set;}
//这将是“PrimaryEmail”与
//“ForwardFrom”列。
公共AdObject ForwardFromObject{get;set;}
}
我想我已经弄明白了。我在理解人际关系背后的逻辑方面经历了一段痛苦的时光。我消除了一些误解,在浏览了所有YouTube视频、PluralSight课程和Udemy课程后,我发现它终于开始点击了。我通常在自学这些东西时不会遇到这么大的困难,但我的错误观念给我指明了错误的方向。最后,我只需指定键和两个关系(其余部分由约定完成)
公共类AdObject
{
[键,列(顺序=0)]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
公共字符串对象类型{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串DisplayName{get;set;}
公共字符串区分名称{get;set;}
[键,列(顺序=1)]
公共字符串PrimaryEmail{get;set;}
公共字符串别名{get;set;}
公共字符串SamAccountName{get;set;}
公共字符串PrimaryDisplay{get;set;}
公共字符串CanonicalName{get;set;}
公共字符串OU{get;set;}
公共字符串核心组{get;set;}
公共布尔?已禁用{get;set;}
公共bool?IsForwarded{get;set;}
公共bool?deliverandDirect{get;set;}
公共bool?DisableForwardAtLogon{get;set;}
公共日期时间?在{get;set;}
公共字符串通知{get;set;}
公共日期时间?LastLogon{get;set;}
公共日期时间?LastApiLogon{get;set;}
公共日期时间?LastCheck{get;set;}
public AdObject ForwardedToObject{get;set;}
公共ICollection ForwardRecipientSchedule{get;set;}
=新列表();
公共ICollection ForwardSchedule{get;set;}
=新列表();
}
公开课前进
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
公共AdObject ForwardFromObject{get;set;}
公共AdObject ForwardToObject{get;set;}
公共bool?deliverandDirect{get;set;}
公共日期时间?开始时间{get;set;}
公共日期时间?停止时间{get;set;}
公共字符串重复{get;set;}
公共bool?DisableForwardAtLogon{get;set;}
公共日期时间?在{get;set;}
公共字符串通知{get;set;}
公共字符串StartJobId{get;set;}
公共字符串StopJobId{get;set;}
公共字符串StartJobStatus{get;set;}
公共字符串StopJobStatus{get;set;}
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);
}
}