0

Im trying to save some objects to the database when I get the error above. All I have googled and searched has not yet resulted in something useful, therefore the question here.

The code where it goes wrong:

foreach (Contract con in sub.Contracts.Where(c => !c.Approved))
        {
            con.Approved = true;
            DatabaseHelper.db.Contracts.Add(con);
        }

The class Contract:

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;
    }
}

And the class subscription:

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

The error occurs when I try to add an extra Contract with the same Subscription. In the situation I have 2 Contracts that are already added and are proxy instances and 2 Contracts that are still model instances. When I start adding the model instances the error is thrown. The weird thing is that my proxy instances also have the same Subscription pointed to it and that does not throw an error. The only thing I can think of is that the model that is trying to be added also has the same name and Administration.

I hope anyone knows a solution to this nagging problem. The slight idea I have is that Entity Framework does not recognise my one to many relationship between subscription and contract. Why it would not recognise this relationship is something I don't get.

thx in advance!

Gijs Kuijer
  • 80
  • 2
  • 7

1 Answers1

2

Ok I got a hold of the solution while cycling to my work today.

In my application I got two way bindings. I updated the binding on the side of the Subscription by copying the Contracts of the duplicate not database attached Subscription to the database attached Subscription.

What I did not notice is that the Subscription on the Contract items was still the non-attached Subscription. So as I was trying to add the Contract to the database EF was cascading and also tried to add the non-attached Subscription to the database. This of course then does not make sense as one Contract then has an unsynced binding. EF threw the multiplicity error as the Subscription that it was also adding together with the Contract violated the multiplicity constraint as two Subscriptions were now referencing the same Contract.

Gijs Kuijer
  • 80
  • 2
  • 7