BLToolkit 中的字段映射到类类型属性
我的表架构(摘录)
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
)
实体类
[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; }
}
有问题吗?
将字段映射到我的类是问题所在。我想直接从来自数据表(顶部)的平面结果集中映射内部类属性。
我已经在类级别设置了 MapFieldAttribute 设置(如上面的代码所示),但我的规则始终为空。假设问题的一部分是必须首先实例化此内部类属性才能分配给它,因为所有 BLToolkit 示例都使用不可为 null 的内部对象。但就我而言,如果它应该为 null
(大多数情况下它会是 null
),我不想创建它的实例。
那该怎么办呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工作溶液
我只是测试了可能与此有些相关的各种属性,实际上我已经使它起作用了。
如果您希望嵌套对象按预期工作,则必须使用额外的
NoInstanceAttribute
。并且您必须像以前一样将这些字段映射属性保留在类上。生成的工作代码如下:所有未定义值的规则均为空,其他规则被实例化。
Working solution
I was just testing various attributes that may be somewhat related to this and actually I've made it work.
If you want nested objects to work as expected you have to use an additional
NoInstanceAttribute
. And you have to keep those field mapping attributes on the class as before. The resulting working code is as follows:All rules that don't define values are null, others are instantiated.
BLToolkit 不会创建 MyEntityRule 的实例,您必须自己创建。
BLToolkit does not create an instance of MyEntityRule You have to do it yourself..