My table schema (excerpt)
create table dbo.MyEntity
(
MyEntityID int identity not null
primary key,
Name nvarchar(50) not null
unique,
Description nvarchar(500) null,
-- these two are optional fields
MaxCount int null,
MinSpace int null
)
Entity class(es)
[MapField("MaxCount", "Rule.MaxCount")]
[MapField("MinSpace", "Rule.MinSpace")]
public class MyEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
// when values are not null this property should have an instance
public MyEntityRule Rule { get; set; }
public bool HasRule
{
get { return this.Rule != null; }
}
}
public class MyEntityRule
{
public int MaxCount { get; set; }
public int MinSpace { get; set; }
}
Problem?
Mapping of fields to my class is the problem. I would like to directly map inner class properties from flat results set that comes from data table (at the top).
I've set MapFieldAttribute
settings at class level (as seen in upper code), but my rules are always null. Is suppose part of the problem is that this inner class property has to be instantiated first to be assigned to, because all BLToolkit examples use non nullable inner objects. But in my case I don't want to create an instance of it if it should be null
(most of the time it will be null
).
How should this be done then?