0
votes

I have some problems when updating my model. This is my update code:

public void Update(Class @class)
{
    var updatedClass = context.Classes.Where(c => c.ClassId == @class.ClassId).FirstOrDefault();
    updatedClass.ClassPriceTypeId = @class.ClassPriceTypeId;
    updatedClass.ClassType = @class.ClassType;
    updatedClass.Name = @class.Name;
    updatedClass.Title = @class.Title;
    updatedClass.MetaTag = @class.MetaTag;
    updatedClass.MetaDescription = @class.MetaDescription;
    updatedClass.UrlSafe = @class.UrlSafe;
    updatedClass.Header = @class.Header;
    updatedClass.Margin = @class.Margin;
    updatedClass.ImageName = @class.ImageName;
    updatedClass.GroupId = @class.GroupId;
    updatedClass.IsPublished = @class.IsPublished;
    context.SaveChanges();
}

First problem is that I have made LazyLoadingEnabled=false but after fetching updatedClass the relational properties like Group is not null.

Second problem is that in some of @class objects I can easily update my entity but in some others I see this error:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

Update

This is Class model:

public class Class
{
    public int ClassId { get; set; }
    public int GroupId { get; set; }
    public int ClassPriceTypeId { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string MetaTag { get; set; }
    public string MetaDescription { get; set; }
    public string UrlSafe { get; set; }
    public string Header { get; set; }
    public string ImageName { get; set; }
    public int Margin { get; set; }
    public string ClassType { get; set; }
    public bool IsPublished { get; set; }
    public virtual Group Group { get; set; }
    public virtual List<Product> Products { get; set; }
    public virtual List<Comparing.Model.Price.Price > Prices { get; set; }
    public virtual ClassPriceType.ClassPriceType ClassPriceType { get; set; }
    public virtual List<Garanty> Garanties { get; set; }
    public virtual List<PhoneModel> PhoneModels { get; set; }
    public virtual List<ClassPartner> ClassPartners { get; set; }
    public virtual List<Content> Contents { get; set; }
}

And this is Group model:

public class Group
{
    public int GroupId { get; set; }
    public int SectionId { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string MetaTag { get; set; }
    public string MetaDescription { get; set; }
    public string UrlSafe { get; set; }
    public string Header { get; set; }
    public string ImageName { get; set; }
    public bool IsPublished { get; set; }
    public virtual Section Section { get; set; }
    public virtual List<Class> Classes { get; set; }
}

Can anyone help me about the problem?

1
Show the Class and Group class.. - Yuliam Chandra
did you happen to nullify the Group property ? try put context.Configuration.ProxyCreationEnabled = false; in the beginning of the method - Yuliam Chandra
I had these lines in my context: base.Configuration.LazyLoadingEnabled = false; base.Configuration.ProxyCreationEnabled = false; base.Configuration.ValidateOnSaveEnabled = false; But its not working yet. - Hamid Reza
the only cause that I can think of is its a proxy and somehow the Group is set to null, if the proxy is disabled, this should be solved, where do you put the configuration? on parameterless constructor ? or there is another constructor ? - Yuliam Chandra
The configuration is in my Context class constructor. - Hamid Reza

1 Answers

0
votes

I guess, you are trying to update a primary key field which is having a relation ship with other foreign key with not null constraint. I guess you need to use update cascade.