初用Linq To Sql
Project1 从SQL生成了实体类
Project2引用Project1, 是个ConsoleApp:
TestDataContext db = new DakkaDataContext();
Employee em = new Employee("aaa", "bbb");
db.Employee.InsertOnSubmit(em);
db.SubmitChanges();
运行后抛空引用异常:
在 System.Data.Linq.Mapping.EntitySetDefSourceAccessor`2.GetValue(T instance)
在 System.Data.Linq.Mapping.MetaAccessor`2.GetBoxedValue(Object instance)
在 System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.HasDeferredLoader(MetaDataMember deferredMember)
在 System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.get_HasDeferredLoaders()
在 System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(MetaType mt, Object obj, Dictionary`2 visited, Boolean recurse, Int32 level)
在 System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(Object obj, Boolean recurse)
在 System.Data.Linq.ChangeTracker.StandardChangeTracker.Track(Object obj)
在 System.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)
在 Dakka.Test.ConsoleApp.Program.Main(String[] args) 位置 ****\ConsoleApp\Program.cs:行号 20
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
请指教, 谢谢!
EntitySet properties 没有初始化造成的。
提供你一个捕获异常代码。这样每个冲突的实体和成员信息会显示在控制台窗口中。
Code
TestDataContext db = new DakkaDataContext();
Employee em = new Employee("aaa", "bbb");
db.Employee.InsertOnSubmit(em);
try
{
db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException e)
{
Console.WriteLine("Optimistic concurrency error.");
Console.WriteLine(e.Message);
Console.ReadLine();
foreach (ObjectChangeConflict occ in db.ChangeConflicts)
{
MetaTable metatable = db.Mapping.GetTable(occ.Object.GetType());
Customer entityInConflict = (Customer)occ.Object;
Console.WriteLine("Table name: {0}", metatable.TableName);
Console.Write("Customer ID: ");
Console.WriteLine(entityInConflict.CustomerID);
foreach (MemberChangeConflict mcc in occ.MemberConflicts)
{
object currVal = mcc.CurrentValue;
object origVal = mcc.OriginalValue;
object databaseVal = mcc.DatabaseValue;
MemberInfo mi = mcc.Member;
Console.WriteLine("Member: {0}", mi.Name);
Console.WriteLine("current value: {0}", currVal);
Console.WriteLine("original value: {0}", origVal);
Console.WriteLine("database value: {0}", databaseVal);
}
}
}
catch (Exception ee)
{
// Catch other exceptions.
Console.WriteLine(ee.Message);
}
finally
{
Console.WriteLine("TryCatch block has finished.");
}
请把异常信息写得详细些,主要是Message显示为何内容