Error saving data with EF

Asked

Viewed 95 times

0

I am assembling my unit tests and trying to save the data in a table using the Entity Framework, but at the time I run it returns an error.

Validation failed for one or more entities. See 'Entityvalidationerrors' Property for more Details.

inserir a descrição da imagem aqui

in System.Data.Entity.Internal.Internalcontext.Savechanges() in System.Data.Entity.Internal.Lazyinternalcontext.Savechanges() in System.Data.Entity.DbContext.Savechanges() in Carrinhodecompras.Infra.Data.EF.Repositorios.Repositoriobase`1.Add(Tentity obj) in C: WORK 1-Files Personal Cartdecompras Cartdecompras.Infra.Data.EF Repositorios Repositoriobase.Cs:line 17 in Cartdecompras.Application.Test.OrdersTest.Inclirpedidos() in C: WORK 1-Archives Personal Cartdecompras Carrinhodecompras.Application.Test Orderstest.Cs:line 53

Analyzed I saw that the EF seems to be trying to make an Insert in a dependency table that I am trying to insert the data.

using CarrinhoDeCompras.Domain.Entidades;
using CarrinhoDeCompras.Domain.Interfaces.Repositorios;
using CarrinhoDeCompras.Infra.Data.EF.Repositorios;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace CarrinhoDeCompras.Application.Test
{
    [TestClass]
    public class OrdersTest
    {
        private  IRepositorioOrders _repositorio;
        [TestMethod]
        public void InclirPedidos()
        {
            var order = new Orders()
            {
                CustomerID = 85,
                Customer = "VINET",
                EmployeeID = 5,
                OrderDate = DateTime.Now,
                RequiredDate = DateTime.Now,
                ShippedDate = DateTime.Now,
                ShipVia = 3,
                Freight = 13,
                ShipName = "Vins et alcools Chevalier",
                ShipAddress = "59 rue de l'Abbaye",
                ShipCity = "Reims",
                ShipPostalCode = "51100",
                ShipCountry = "France",
            };


            try
            {
                _repositorio = new RepositorioOrders();
                _repositorio.Add(order);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}

As you can see I am passing the Ids of dependency tables as the CustomerID = 85, , but still he’s complaining about that field.

My Classes Orders.

public partial class Orders
    {
        public Orders()
        {
            this.OrderDetails = new HashSet<OrderDetails>();
            this.Employees = new Employees();
            this.Customers = new Customers();
            this.Shippers = new Shippers();
        }

        public int OrderID { get; set; }
        public int? CustomerID { get; set; }
        public string Customer { get; set; }
        public int? EmployeeID { get; set; }
        public DateTime? OrderDate { get; set; }
        public DateTime? RequiredDate { get; set; }
        public DateTime? ShippedDate { get; set; }
        public int ShipVia { get; set; }
        public decimal? Freight { get; set; }
        public string ShipName { get; set; }
        public string ShipAddress { get; set; }
        public string ShipCity { get; set; }
        public string ShipRegion { get; set; }
        public string ShipPostalCode { get; set; }
        public string ShipCountry { get; set; }
        public virtual Customers Customers { get; set; }
        public virtual Employees Employees { get; set; }
        public virtual ICollection<OrderDetails> OrderDetails { get; set; }
        public virtual Shippers Shippers { get; set; }
    }
  • In EF Mapping it is not necessary to Ignore the Validationresults property. :)

  • See here: https://github.com/thiagolunardi/MvcMusicStoreDDD/blob/abc8a02506dd05bbc6ea40cc81d7c5b2ecea0817/MvcMusicStore.Data.Context/Mapping/OrderMap.cs#L56

  • No, I’ll implement it here to see.

  • A lot to implement to use your Validationresults

  • Just tell EF to ignore properties that do not persist.

  • I tried to do this but ... The navigation Property 'Orders' is not a declared Property on type 'Customers'. Verify that it has not been explicitly excluded from the model and that it is a Valid navigation Property.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.