为什么在entityframework.dll中出现“system.invalidoperationexception”类型的异常,但在保存时未在用户代码中处理

js5cn81o  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(307)

例外情况:
其他信息:操作失败:无法更改关系,因为一个或多个外键属性不可为null。对关系进行更改时,相关的外键属性设置为空值。如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。
有2个外键与我的事件表相关,1。维努埃和2。注册状态ID
下面是我的代码。

var _context = new EventbriteContext(appKey, userKey);
expo.eventbrite.EventService service = new eventbrite.EventService(_context);
var ebEvents = service.FindByOrganizer(organizerId);
pacificCurrentDateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
AutoMapperConfigurator.Configure();
Mapper.AssertConfigurationIsValid();
foreach (var item in ebEvents)
{
    var dbEvent = uow.EventRepository.Find(x => x.EventbriteId == item.Id).FirstOrDefault();
    if (dbEvent == null)
    {
        dbEvent = Mapper.Map<Event>(item);
        uow.EventRepository.Insert(dbEvent);
    }
    else
    {
        var newEvent = Mapper.Map<model.Event>(item);
        string Startdate = (newEvent.StartDate).ToString("yyyy,MM,dd");
        string Enddate = (newEvent.EndDate).ToString("yyyy,MM,dd");
        string currentdate = DateTime.Now.ToString("yyyy,MM,dd");
        if (newEvent.RegistrationStatusId != 2)
        {
            if (dbEvent.RegistrationStatusId != newEvent.RegistrationStatusId)
            {
                dbEvent.Title = newEvent.Title;
                dbEvent.Description = newEvent.Description;
                dbEvent.RegistrationStatusId = newEvent.RegistrationStatusId;
                uow.Save();
            }
            continue;
        }
        dbEvent.Title = newEvent.Title;
        dbEvent.Description = newEvent.Description;
        dbEvent.StartDate = newEvent.StartDate;
        dbEvent.EndDate = newEvent.EndDate;
        dbEvent.EventbriteId = newEvent.EventbriteId;
        dbEvent.RegistrationStatusId = newEvent.RegistrationStatusId;
        dbEvent.Venue.Address = newEvent.Venue.Address;
        dbEvent.Venue.Address2 = newEvent.Venue.Address2;
        dbEvent.Venue.City = newEvent.Venue.City;
        dbEvent.Venue.Country = newEvent.Venue.Country;
        dbEvent.Venue.Name = newEvent.Venue.Name;
        dbEvent.Venue.PostalCode = newEvent.Venue.PostalCode;
        dbEvent.Venue.EventbriteID = newEvent.Venue.EventbriteID;
        newEvent.Tickets.ForEach(x => {
            var existingTicket = dbEvent.Tickets.FirstOrDefault(y => y.EventbriteId == x.EventbriteId);
            if (existingTicket != null)
            {
                x.Id = existingTicket.Id;
                existingTicket.Name = x.Name;
            }
        });
        dbEvent.Tickets.MergeUpdate(newEvent.Tickets, uow.EventRepository.context,
        (destination, source) => {
            destination.Description = source.Description;
            destination.EndDateTime = source.EndDateTime;
            destination.QuantityAvailable = source.QuantityAvailable;
            destination.QuantitySold = source.QuantitySold;
            destination.StartDateTime = source.StartDateTime;
            destination.LastSync = pacificCurrentDateTime;
        });
    }
    dbEvent.LastSync = pacificCurrentDateTime;
    uow.Save();
}

下面是班级

namespace expo.model
 {

    [Table("Events")]
    public class Event : Entity, IEventbriteEntity
    {

        public string Title { get; set; }
        public string Description { get; set; }
        public string Category { get; set; }
        public string Tags { get; set; }
        public long EventbriteId { get; set; }
        public DateTime LastSync { get; set; }
        public Nullable<DateTime> LastAttendeeSync { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public string Timezone { get; set; }
        public string Location { get; set; }
        public string Url { get; set; }
        public string Logo { get; set; }
        public string LogoSSL { get; set; }
        public int? VenueId { get; set; }
        public int RegistrationStatusId { get; set; }

        [ForeignKey("VenueId")]
        public virtual Venue Venue { get; set; }

        [ForeignKey("RegistrationStatusId")]
        public virtual RegistrationStatus RegistrationStatus { get; set; }
        public virtual List<Ticket> Tickets { get; set; }
        public virtual List<Order> Orders { get; set; }

        public virtual List<Question> Questions { get; set; }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题