T
Tapio Kulmala
Hi!
Here is a simple console-program that illustrates the problem. It uses
Northwind database and requires only Customer and Order entities.
The problem is that if the first datacontext.SubmitChanges fails because
of validation errors also the second one will fail. It seems that the
second datacontext.SubmitChanges tries to validate also the first order
and that will of course fail.
What should I do to recover from the first failure? The failing order is
somewhere in the cache and I don't know how to get rid of it. I know I
could create a new datacontext instance but in my case it would require
major refactorings.
Any Ideas?
Tapio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqProblemDemo
{
class Program
{
static void Main(string[] args)
{
NorthwindDataClassesDataContext datacontext = new
NorthwindDataClassesDataContext();
Customer customer1 = datacontext.Customers.Single<Customer>
(c => c.CustomerID == "BONAP");
Customer customer2 = datacontext.Customers.Single<Customer>
(c => c.CustomerID == "BOLID");
try
{
// fails
Order order1 = new Order();
customer1.Orders.Add(order1);
datacontext.SubmitChanges();
}
catch ( Exception ex)
{
System.Console.WriteLine(ex.Message);
}
try
{
// shouldn't fail but fails
Order order2 = new Order();
order2.OrderDate = DateTime.Now;
customer2.Orders.Add(order2);
datacontext.SubmitChanges();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
System.Console.ReadLine();
}
}
partial class Order
{
partial void OnValidate()
{
if (!this.OrderDate.HasValue)
{
throw new ApplicationException("OrderDate was missing.
Customer : " + this.CustomerID);
}
}
}
}
Here is a simple console-program that illustrates the problem. It uses
Northwind database and requires only Customer and Order entities.
The problem is that if the first datacontext.SubmitChanges fails because
of validation errors also the second one will fail. It seems that the
second datacontext.SubmitChanges tries to validate also the first order
and that will of course fail.
What should I do to recover from the first failure? The failing order is
somewhere in the cache and I don't know how to get rid of it. I know I
could create a new datacontext instance but in my case it would require
major refactorings.
Any Ideas?
Tapio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqProblemDemo
{
class Program
{
static void Main(string[] args)
{
NorthwindDataClassesDataContext datacontext = new
NorthwindDataClassesDataContext();
Customer customer1 = datacontext.Customers.Single<Customer>
(c => c.CustomerID == "BONAP");
Customer customer2 = datacontext.Customers.Single<Customer>
(c => c.CustomerID == "BOLID");
try
{
// fails
Order order1 = new Order();
customer1.Orders.Add(order1);
datacontext.SubmitChanges();
}
catch ( Exception ex)
{
System.Console.WriteLine(ex.Message);
}
try
{
// shouldn't fail but fails
Order order2 = new Order();
order2.OrderDate = DateTime.Now;
customer2.Orders.Add(order2);
datacontext.SubmitChanges();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
System.Console.ReadLine();
}
}
partial class Order
{
partial void OnValidate()
{
if (!this.OrderDate.HasValue)
{
throw new ApplicationException("OrderDate was missing.
Customer : " + this.CustomerID);
}
}
}
}