C# 多对多关系只得到部分解决
首先,我在EF5代码方面遇到了一些问题,主要是因为我不知道我必须寻找什么样的行为 它是关于编写日历后端的 日历包括日历条目列表 日历由用户拥有 CalendarEntry具有所有者(与其日历相同) CalendarEntry具有邀请的可选列表 用户拥有其所有邀请的列表 问题是: 当我将用户添加到一个日历条目时,它可以工作,但是当一个用户想要将同一用户添加到另一个条目时,第一个日历条目会丢失该用户(CalendarEnty.invites) 另一方面,正确列出了用户的邀请量,并且邀请表具有正确的值 有没有关于如何解决这个问题的提示C# 多对多关系只得到部分解决,c#,entity-framework,code-first,C#,Entity Framework,Code First,首先,我在EF5代码方面遇到了一些问题,主要是因为我不知道我必须寻找什么样的行为 它是关于编写日历后端的 日历包括日历条目列表 日历由用户拥有 CalendarEntry具有所有者(与其日历相同) CalendarEntry具有邀请的可选列表 用户拥有其所有邀请的列表 问题是: 当我将用户添加到一个日历条目时,它可以工作,但是当一个用户想要将同一用户添加到另一个条目时,第一个日历条目会丢失该用户(CalendarEnty.invites) 另一方面,正确列出了用户的邀请量,并且邀请表具有正确的值
public class Calendar
{
[Key]
public int CalendarId { get; set; }
[Required]
public virtual User Owner { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<CalendarEntry> CalendarEntries { get; set; }
}
public class CalendarEntry
{
public CalendarEntry()
{
Invitees = new List<User>();
}
public virtual ICollection<User> Invitees { get; set; }
public bool IsEmpty
{
get
{
if (Invitees.Count == 0)
{
return true;
}
return false;
}
}
[Key]
public int CalendarEntryId { get; set; }
[Required]
public DateTime StartDate { get; set; }
[Required]
public DateTime EndDate { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public double Duration
{
get
{
return EndDate.Subtract(StartDate).TotalMinutes;
}
}
public int OwnerId { get; set; }
public virtual User Owner { get; set; }
public int CalendarId { get; set; }
public virtual Calendar Calendar { get; set; }
public virtual Room Room { get; set; }
}
public class Invite
{
[Key]
public int InviteId { get; set; }
public virtual CalendarEntry CalendarEntry { get; set; }
public int Accepted { get; set; }
public virtual User Owner {get; set;}
}
public class User
{
[Key]
public int UserId { get; set; }
[Required]
public string GivenName { get; set; }
[Required]
public string Surname { get; set; }
[Required]
public string MailAddress { get; set; }
public string PhoneNumber { get; set; }
public virtual Calendar Calendar { get; set; }
public int? CalendarId { get; set; }
//SHA512Hashed
private string _password = null;
public string Password
{
get
{
return _password;
}
set
{
_password = value;
}
}
public virtual ICollection<Group> Groups { get; set; }
public virtual ICollection<Invite> Invites { get; set; }
}
您没有显示您的用户模型,但我怀疑您没有在其中声明对(一组)CalendarEntry模型的引用。请确保您已经声明了如下内容:
public class User
{
public ICollection<CalendarEntry> CalendarEntries { get; set; }
// ... other User properties
}
应该是:
public virtual ICollection<Invite> Invitations { get; set; }
公共虚拟ICollection邀请{get;set;}
要获得您期望的行为(例如,让一个
用户通过Invite映射对象与多个CalendarEntry对象关联)。您没有显示您的用户模型,但我怀疑您没有在其中声明对(一组)CalendarEntry模型的引用。请确保您已经声明了如下内容:
public class User
{
public ICollection<CalendarEntry> CalendarEntries { get; set; }
// ... other User properties
}
应该是:
public virtual ICollection<Invite> Invitations { get; set; }
公共虚拟ICollection邀请{get;set;}
获取您期望的行为(例如,让用户通过邀请映射对象与多个CalendarEntry对象关联)。您好,感谢您的快速响应!用户模型是文章的一部分。不幸的是,您必须在代码块内滚动。我会设法解决的。关于你的提示:我一定会尝试的!非常感谢!有一件事我不清楚。在我看来,用户和日历之间没有多对多的关系。因为这应该存在于CalendarEntry和基于邀请模型的用户之间。我错了吗?如果我错过了你剩下的代码块,我很抱歉。我看到您有一个Invite
类,它充当User
和CalendarEntry
之间多对多关系的映射表。因为您指定的是显式映射表,所以在CalendarEntry
对象中不需要ICollection inviteed
。您应该有一个i收集邀请
或类似的东西。当您想查找已被邀请到日历条目
的用户时,您可以在邀请
对象中导航。您好,感谢您的快速响应!用户模型是文章的一部分。不幸的是,您必须在代码块内滚动。我会设法解决的。关于你的提示:我一定会尝试的!非常感谢!有一件事我不清楚。在我看来,用户和日历之间没有多对多的关系。因为这应该存在于CalendarEntry和基于邀请模型的用户之间。我错了吗?如果我错过了你剩下的代码块,我很抱歉。我看到您有一个Invite
类,它充当User
和CalendarEntry
之间多对多关系的映射表。因为您指定的是显式映射表,所以在CalendarEntry
对象中不需要ICollection inviteed
。您应该有一个i收集邀请
或类似的东西。当您想查找已被邀请到日历条目
的用户时,您可以在邀请
对象中导航。