开发者

LINQ to SQL ForeignKeyReferenceAlreadyHasValueException error

I know what the problem is with updating the foreign key, I just can't seem to make it work.

I tried both the current code, and the line that is commented out.

Sponsorship mySponsorship = Repository._context.Sponsorships.SingleOrDefault<Sponsorship>((Sponsorship obj) => obj.id == int.Parse(editId) ? true : false);

        mySponsorship.Name = txbSponsorshipTitle.Text.Trim();

        DataClassesDataContext db = new DataClassesDataContext();

        // Don't assign just the id
        Event myValue = db.Events.Single(x => x.EventId == int.Parse(dropEvent.SelectedValue));

        int myEvent = myValue.EventId;
        //int myEvent = db.Events.Single(x => x.EventId == int.Parse(dropEvent.SelectedValue)).EventId;
        mySponsorship.EventId = myEvent;
        mySponsorship.Price = decimal.Parse(txbPrice.Text);
        mySponsorship开发者_开发技巧.description = venuEdit.Content;

        Repository._context.SubmitChanges();

        Response.Redirect("Sponsorshiplisting.aspx");


try to assign the event object it self not the EventID

Event myValue = db.Events.Single(x => x.EventId == int.Parse(dropEvent.SelectedValue));
mySponsorship.Event = myValue ;

and you may consider that mySponsorship and event must come from same datacontext otherwise LINQ will create a new identical Event entity to the one you assigned and insert it to database

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜