Resolved Validation failed for one or more entities

LegitMeme

Member
Joined
May 5, 2019
Messages
13
Programming Experience
Beginner
I am trying to create a website where you can order pizza's. I'm currently following a course from Bob Tabor and I've encountered 2 problems (2nd one solved):

problem 1 *Solved? (See Edit 2)*: When I try to save the Orders (that the customer made) to my database it gives me this exception:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

And shows me this page:


These lines of code run right before the Exception:

Inside [Solution.web]:
protected void Page_Load(object sender, EventArgs e)
{
    CustomerRepository.SaveCustomers();
    OrderRepository.SaveOrders();
}

Inside [Solution.domain]:
public static void SaveCustomers()
{
    persistance.CustomerExtract.InputCustomer(currentCustomer);
}

&

public static void SaveOrders()
{
    persistance.OrderExtract.InputOrders(currentOrders);
}

Inside [Solution.Persistance]:
public static void InputCustomer(DTO.Customer DTOcustomer)
{
    Customer customer = new Customer() { Adress = DTOcustomer.Adress, Id = DTOcustomer.Id, Name = DTOcustomer.Name, Phone = DTOcustomer.Phone, Zip = DTOcustomer.Zip };

    PapaBobsDBEntities db = new PapaBobsDBEntities();
    db.Customers.Add(customer);
    db.SaveChanges();
}

&

public static void InputOrders(List<DTO.Order> DTOorders)
{
    PapaBobsDBEntities db = new PapaBobsDBEntities();
    List<Order> orders = new List<Order>();

     foreach (var DTOorder in DTOorders)
    {
        orders.Add(new Order { Cost = DTOorder.Cost, Crust = (persistance.Crust)DTOorder.Crust, Id = DTOorder.Id, Size = (persistance.Size)DTOorder.Size, Toppings = DTOorder.Toppings });
    }

    foreach (var order in orders)
    {
        db.Orders.Add(order);
    }
    db.SaveChanges();
}

Edit:
I defined all the value's of the Orders/Customer by user input and created the Id within the code (Customer's id is a normal Guid)
How I created the id of the Orders:

C#:
private static string getId()
{
    HighestAmmount += 1;

    return domain.CustomerRepository.currentCustomer.Id + "." + HighestAmmount.ToString();
}

Edit 2:
It seems like the Id of the Order was limited to 5 characters which I forgot to set to 50!
I'll try to fix this and see if it solves this exception and leave a edit if it does.

Edit 3:
I solved it! To anyone reading this with the same problem: Check the paramiters of your sql database values before posting like me
and wasting peoples times. Thanks for creating this website I solve a lot while typing.

If you need any more information (which you prob do), please tell me to provide it!

Problem 2 *Solved*:

When I create a new Guid, it comes out as a ton of 0's? (When i used newID() in the sql table default section and when I create a new Guid in code.
Edit: I found this post: Guid is all 0's (zeros)? and fixed it!
Use the static method Guid.NewGuid() instead of calling the default constructor.
var responseObject = proxy.CallService(new RequestObject
{
Data = "misc. data",
Guid = Guid.NewGuid()
});



Thanks for the reading (And especially if you help).
 
Last edited:
Back
Top Bottom