我使用NPoco从我的数据库进行对象映射。我有以下实体:
public abstract class NamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Person : NamedEntity
{
public Office Office { get; set; }
}
public class Office : NamedEntity
{
public Address Address { get; set; }
public Organisation ParentOrganisation { get; set; }
}
public class Address
{
public string AddressLine1 { get; set; }
}
public class Organisation : NamedEntity
{
}我正在使用存储库中的NPoco检索对象:
var people = Context.Fetch<Person, Office, Address, Organisation>(sql);这很好,除非Person没有Office,在这种情况下,LEFT JOIN在LEFT JOIN查询中返回null,用于Office和Organisation。
在这种情况下,将在NPoco中引发未处理的异常:
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation.
---> System.NullReferenceException:
Object reference not set to an instance of an object.
at poco_automapper(Person , Office , Address , Organisation )
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at NPoco.MultiPocoFactory.CallCallback[TRet](Delegate callback, IDataReader dr, Int32 count)
at NPoco.MultiPocoFactory.<>c__DisplayClassa`1.<CreateMultiPocoFactory>b__9(IDataReader reader, Delegate arg3)
at NPoco.Database.<Query>d__14`1.MoveNext()有办法处理这种情况吗?或者我将不得不求助于一个扁平的对象,或者单独的数据库调用?
发布于 2013-09-25 23:56:28
这已在NPoco 2.2.40中得到修正。
谢谢你的报道。
发布于 2013-09-24 16:57:41
尝试创建一个构造函数来初始化对象:
public abstract class NamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Person : NamedEntity
{
public Person()
{
Office = new Office();
}
public Office Office { get; set; }
}
public class Office : NamedEntity
{
public Office()
{
Address = new Address();
ParentOrganisation = new Organisation();
}
public Address Address { get; set; }
public Organisation ParentOrganisation { get; set; }
}
public class Address
{
public string AddressLine1 { get; set; }
}
public class Organisation : NamedEntity
{
}https://stackoverflow.com/questions/18987009
复制相似问题