当我收到上述错误时,我试图将一些对象保存到数据库中。我用谷歌搜索和搜索的所有内容都没有产生有用的东西,因此这里的问题。
出错的代码:
foreach (Contract con in sub.Contracts.Where(c => !c.Approved))
{
con.Approved = true;
DatabaseHelper.db.Contracts.Add(con);
}
类合同:
public class Contract
{
public int ID { get; set; }
public int SubscriptionID { get; set; }
public int? AdministrationID { get; set; }
[Required]
[DataType(DataType.Text)]
public string Name { get; set; }
[Required]
[DataType(DataType.Text)]
public string Description { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime BeginDate { get; set; }
public DateTime EndDate { get; set; }
[Required]
public Boolean Periodically { get; set; }
[Required]
public Boolean Approved { get; set; }
[Required]
public double Amount { get; set; }
[InverseProperty("Contracts")]
[ForeignKey("SubscriptionID")]
public virtual Subscription Subscription { get; set; }
[ForeignKey("AdministrationID")]
public virtual Administration Administration { get; set; }
public Contract()
{
Approved = false;
BeginDate = (DateTime)SqlDateTime.MinValue;
EndDate = (DateTime)SqlDateTime.MinValue;
}
}
和课程订阅:
public enum Status
{
REGISTERED, AUTHORIZED, ADMSELECT, SETTINGSCOMPLETE, TRIAL, FULL
}
public enum Process
{
REGISTERING, CHANGESETTINGS, REGISTERED, UNAUTHORIZED, CANCELLED
}
public class Subscription
{
public int ID { get; set; }
[ForeignKey("Customer")]
public int CustomerID { get; set; }
[ForeignKey("App")]
public int AppID { get; set; }
[DataType(DataType.Text)]
public string ExactFullName { get; set; }
[DataType(DataType.Text)]
public string ExactUserID { get; set; }
[DataType(DataType.Text)]
public string ExactUserName { get; set; }
[DataType(DataType.EmailAddress)]
public string ExactUserEmail { get; set; }
[DataType(DataType.Text)]
public string ExactLanguageCode { get; set; }
[DataType(DataType.Text)]
public string ExactCurrentDivision { get; set; }
[DataType(DataType.Text)]
public string BankAccount { get; set; }
public Boolean PaymentInfoNeeded { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime SubscribedAt { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime PaidForTill { get; set; }
[DataType(DataType.Date)]
public DateTime DeprovisioningDate { get; set; }
[DataType(DataType.Date)]
public DateTime EndContractDate { get; set; }
[DataType(DataType.Text)]
public string AuthorizationCode { get; set; }
[DataType(DataType.DateTime)]
public DateTime AuthorizationDate { get; set; }
[DataType(DataType.Text)]
public string RefreshToken { get; set; }
[DataType(DataType.Text)]
public string AuthorizationToken { get; set; }
[DataType(DataType.DateTime)]
public DateTime tokenExpiresAt { get; set; }
public Status Status { get; set; }
public Process Process { get; set; }
public Boolean AutoSubscribe { get; set; }
public Boolean Incasso { get; set; }
public Boolean AgreeTerms { get; set; }
public Boolean Copy { get; set; }
public virtual Customer Customer { get; set; }
public virtual App App { get; set; }
public virtual ICollection<Administration> Administrations { get; set; }
public virtual ICollection<SubscriptionSetting> Settings { get; set; }
[InverseProperty("Subscription")]
public virtual ICollection<Contract> Contracts { get; set; }
// some more constructors
当我尝试添加具有相同订阅的额外合同时发生错误。在这种情况下,我有 2 个合同已经添加并且是代理实例,还有 2 个合同仍然是模型实例。当我开始添加模型实例时,会引发错误。奇怪的是,我的代理实例也有相同的 Subscription 指向它并且不会引发错误。我唯一能想到的是,试图添加的模型也具有相同的名称和管理。
我希望任何人都知道这个烦人的问题的解决方案。我的一个小想法是实体框架不承认我订阅和合同之间的一对多关系。为什么它不承认这种关系是我不明白的。
提前谢谢!