EntityState Modified SaveChanges不保存

时间:2016-02-25 12:12:49

标签: c# entity-framework-6

ID

1 个答案:

答案 0 :(得分:1)

这不是一个有效的解决方案 ...而是尝试帮助指出不合适的位...

public static Result SaveCutomer(Customer customer)
{
    using (AshdorEntities context = new AshdorEntities())
    {
        try
        {
            //assumption is that its always a disconnected entity if not you will need to check
            context.Customer.Attach(customer);

            //this seems wrong as well
            Contact con = null;

            customer.enterDate = DateTime.Now;

            //this seems wrong as well
            customer.Contact.dateEnter = customer.enterDate;

            //this seems wrong as well
            con = context.Contact.Add(customer.Contact);

            customer.Contact = null;
            //this seems wrong as well
            customer.customerId = con.contactId;

            if (customer.customerId == 0)//new customer
            {
                context.Entry(customer).State = EntityState.Added 
            }
            else //existing 
            {
                context.Entry(customer).State = EntityState.Modified; 
            }
            context.SaveChanges();
        }
        catch 
        {
            return new Result() { status = false, massege = MassegesResult.ADDING_FAILED };
        }

        return new Result() { status = true, massege = MassegesResult.ADDING_SUCCESSFUL };
    }
}