Fluent NHibernate - ValidationException 实体没有映射的 Id
使用 FNH 生成 scema 时出现以下错误:
ValidationException: The Entity 'TemplateStatusInfo' dows not have an Id mapped.
类映射定义如下:
[DataContract]
public abstract class Template
{
[DataMember]
public virtual int? Id { get; set; }
[DataMember]
public virtual string Title { get; set; }
[DataMember]
public virtual TemplateStatusInfo TemplateStatus { get; set; }
}
[DataContract]
public class TemplateStatusInfo
{
[DataMember]
public virtual List<string> ValidCodes { get; set; }
[DataMember]
public virtual string TemplateError { get; set; }
}
映射:
public class TemplateMap : ClassMap<Template>
{
public const string TableName = "Template";
public TemplateMap()
{
Table(TableName);
Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
Map(x => x.Title).Not.Nullable().Length(150);
DiscriminateSubClassesOnColumn("type");
}
}
public class TemplateStatusInfoMap : ClassMap<TemplateStatusInfo>
{
public TemplateStatusInfoMap()
{
Table(TemplateMap.TableName);
HasMany(x => x.ValidCodes).Table("TemplateValidCodes").KeyColumn("Id").Element("CodeName").AsSet().Cascade.AllDeleteOrphan();
Map(x => x.TemplateError).Length(1000);
}
}
它应该生成两个表“Template”(包含列 - Id、Title、TemplateError)和子表“TemplateValidCodes”(包含列 - Id、CodeName) 。
谁能指导我如何在此处映射 TemplateStatusInfo 的 Id (本质上是 Template.Id)?
Getting following error while generating scema with FNH:
ValidationException: The Entity 'TemplateStatusInfo' dows not have an Id mapped.
Class mapping defined as below:
[DataContract]
public abstract class Template
{
[DataMember]
public virtual int? Id { get; set; }
[DataMember]
public virtual string Title { get; set; }
[DataMember]
public virtual TemplateStatusInfo TemplateStatus { get; set; }
}
[DataContract]
public class TemplateStatusInfo
{
[DataMember]
public virtual List<string> ValidCodes { get; set; }
[DataMember]
public virtual string TemplateError { get; set; }
}
Mappings:
public class TemplateMap : ClassMap<Template>
{
public const string TableName = "Template";
public TemplateMap()
{
Table(TableName);
Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
Map(x => x.Title).Not.Nullable().Length(150);
DiscriminateSubClassesOnColumn("type");
}
}
public class TemplateStatusInfoMap : ClassMap<TemplateStatusInfo>
{
public TemplateStatusInfoMap()
{
Table(TemplateMap.TableName);
HasMany(x => x.ValidCodes).Table("TemplateValidCodes").KeyColumn("Id").Element("CodeName").AsSet().Cascade.AllDeleteOrphan();
Map(x => x.TemplateError).Length(1000);
}
}
It should result in two tables "Template"(with columns- Id, Title, TemplateError) and child table "TemplateValidCodes"(with columns- Id, CodeName).
Can anyone please guide how can I map Id for TemplateStatusInfo here (which will essentially be Template.Id)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您试图建立多(ValidCodes)一(TemplateStatusInfo)关联,但 TemplateStatusInfo 没有 id,那么数据库如何关联这些信息呢?此外,您没有在模板映射中映射 TemplateStatus。
我认为你的数据结构需要重新考虑一下。
也许 TemplateStatusInfo 应该映射为 Template 的组件,而不是作为单独的实体。
试试这个:
You're trying to have a many (ValidCodes ) one (TemplateStatusInfo) association but TemplateStatusInfo doesn't have an id, so how can the database relate these peices of information? Also you did not map TemplateStatus in the Template mapping.
I think your data structure needs a bit of a rethink.
Probably TemplateStatusInfo should be mapped as a component of Template, rather than being a separate entity.
Try this: