Fluent NHibernate - ValidationException 实体没有映射的 Id

发布于 2024-10-04 13:46:00 字数 1543 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2024-10-11 13:46:00

您试图建立多(ValidCodes)一(TemplateStatusInfo)关联,但 TemplateStatusInfo 没有 id,那么数据库如何关联这些信息呢?此外,您没有在模板映射中映射 TemplateStatus。

我认为你的数据结构需要重新考虑一下。

也许 TemplateStatusInfo 应该映射为 Template 的组件,而不是作为单独的实体。

试试这个:

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);
        Component(x => x.TemplateStatus, TemplateStatusInfoComponentMap.Map);          
        DiscriminateSubClassesOnColumn("type");
    }
}

public class TemplateStatusInfoComponentMap
    {
        public static void Map(ComponentPart<TemplateStatusInfo> part)
        {
           part.HasMany(x => x.ValidCodes).Table("TemplateValidCodes").KeyColumn("Id").Element("CodeName").AsSet().Cascade.AllDeleteOrphan();
           part.Map(x => x.TemplateError).Length(1000);
        }
    }

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:

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);
        Component(x => x.TemplateStatus, TemplateStatusInfoComponentMap.Map);          
        DiscriminateSubClassesOnColumn("type");
    }
}

public class TemplateStatusInfoComponentMap
    {
        public static void Map(ComponentPart<TemplateStatusInfo> part)
        {
           part.HasMany(x => x.ValidCodes).Table("TemplateValidCodes").KeyColumn("Id").Element("CodeName").AsSet().Cascade.AllDeleteOrphan();
           part.Map(x => x.TemplateError).Length(1000);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文