I'm setting up my unit tests and tried saving the data to 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.
inSystem.Data.Entity.Internal.InternalContext.SaveChanges()in System.Data.Entity.Internal.LazyInternalContext.SaveChanges()on System.Data.Entity.DbContext.SaveChanges()in ShoppingCart.Infra.Data.EF.Repositorios.RepositorioBase'1.Add(TEntity obj)inC:\WORK\1-Files Personal\ShoppingCart\ShoppingCart.Infra.Data.EF\Repositories\RepositorioBase.cs:line 17inCart.Application.Test.OrdersTest.InclarPedidos() inC:\WORK\1-Files Personal\ShoppingCart\ShoppingCart.Application.Test\OrdersTest.cs:line 53
I'veseenthatEFseemstobetryingtoinsertintoadependencytablefromwhichI'mtryingtoinsertthedata.
usingCarrinhoDeCompras.Domain.Entidades;usingCarrinhoDeCompras.Domain.Interfaces.Repositorios;usingCarrinhoDeCompras.Infra.Data.EF.Repositorios;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingSystem;namespaceCarrinhoDeCompras.Application.Test{[TestClass]publicclassOrdersTest{privateIRepositorioOrders_repositorio;[TestMethod]publicvoidInclirPedidos(){varorder=newOrders(){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'm passing the dependency table IDs as CustomerID = 85,
, but it's still complaining about that field.
My Orders classes.
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; }
}