Question

Je l'ai recherché salut et bas et je suis coincé ici.

J'utilise EF 4.1 dans une application MVC3, avec le motif service / dépôt / UnitOfWork et AutoMapper pour cartographier mes modèles et entités.

J'ai une situation vraiment de base; J'ai une collection de ChildProducts qui ont une collection de PriceTiers.

Mes modèles de vue ressemblent à ceci:

AddEditChildProductModel

public class AddEditChildProductModel
    {
        #region "Fields/Properties"
        public ActionType ActionType { get; set; }
        public string FormAction { get; set; }

        public int ID { get; set; }
        public int ProductID { get; set; }

        public string Sku { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public decimal Cost { get; set; }
        public decimal MSRP { get; set; }
        public decimal RetailPrice { get; set; }

        public int Servings { get; set; }
        public decimal Weight { get; set; }

        public bool Display { get; set; }
        public int DisplayIndex { get; set; }

        public IEnumerable<AddEditPriceTierModel> PriceTiers { get; set; }

        #endregion

        #region "Constructor(s)"

        #endregion

        #region "Methods"

        #endregion
    }

AddEditPriceTierModel

public class AddEditPriceTierModel
    {
        #region "Fields/Properties"
        public int ID { get; set; }
        public int ChildProductID { get; set; }
        public decimal Price { get; set; }
        public int QuantityStart { get; set; }
        public int QuantityEnd { get; set; }
        public bool IsActive { get; set; }
        #endregion

        #region "Constructor(s)"

        #endregion

        #region "Methods"

        #endregion
    }

Dans l'action du contrôleur, je tente simplement de cartographier les propriétés PriceTier changé:

public ActionResult EditChildProduct(AddEditChildProductModel model)
        {
            if (!ModelState.IsValid)
                return PartialView("AddEditChildProduct", model);

            ChildProduct childProduct = productService.GetChildProductByID(model.ID);
            AutoMapper.Mapper.Map<AddEditChildProductModel, ChildProduct>(model, childProduct);
            UnitOfWork.Commit();

            return ListChildProducts(model.ProductID);
        }

Et je reçois cette erreur:

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.

Quand entrer dans l'action, les modèles / entités sont mis en correspondance correctement, je ne comprends pas !!

Était-ce utile?

La solution

Eranga est juste. Je devine que votre ProductService ne remet pas AsNoTracking le contexte ef avant de retourner le ChildProduct. Sinon, ce moyen ce qu'il renvoie est toujours attaché au contexte. Quand automapper fait son travail, il remplace toute la collection, qui peut orphelins les entités enfant attachées qui ne faisaient pas partie de la soumission du formulaire. Étant donné que les orphelins ne sont pas une clé étrangère non nulle, ils doivent être supprimés du contexte avant d'appeler SaveChanges. Si elles ne sont pas, vous obtenez cette exception infâme.

Par contre, si vos appels ProductService AsNoTracking le contexte avant de retourner l'entité, il ne sera pas le suivi des modifications, et ne tentera pas de supprimer tous les éléments orphelins qui n'existent pas dans la collection créée par automapper.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top