Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Nhibernate OneToMany(反向)将FK设置为0,为什么?_C#_Nhibernate_Nhibernate Mapping_Mapping By Code - Fatal编程技术网

C# Nhibernate OneToMany(反向)将FK设置为0,为什么?

C# Nhibernate OneToMany(反向)将FK设置为0,为什么?,c#,nhibernate,nhibernate-mapping,mapping-by-code,C#,Nhibernate,Nhibernate Mapping,Mapping By Code,我的nHibernate映射代码有问题 表内容包含来自FK列ContentId引用的表内容文本的多个条目 如果我在集合中添加一个包含一个文本的新ContentEntry,我会执行对Content表的第一个查询,但对Content_Text的第二个查询失败,因为NHibernate正在尝试插入ContentId=0的Content_文本实体,而该实体在Content表中肯定不存在 以下是我对内容表的映射: public class ContentMap : ClassMapping<TCon

我的nHibernate映射代码有问题

表内容包含来自FK列ContentId引用的表内容文本的多个条目

如果我在集合中添加一个包含一个文本的新ContentEntry,我会执行对Content表的第一个查询,但对Content_Text的第二个查询失败,因为NHibernate正在尝试插入ContentId=0的Content_文本实体,而该实体在Content表中肯定不存在

以下是我对内容表的映射:

public class ContentMap : ClassMapping<TContent>
{
    #region Constructors

    public ContentMap()
    {
        Table("nfcms_Content");
        Schema("dbo");
        Lazy(false);
        Id<int>(x => x.Id, map => {

            map.Generator(Generators.Identity);

        });
        //Property(x => x.Id, map => map.NotNullable(true));

        Property(x => x.Cid, map =>
        {
            map.NotNullable(false);

        });
        Property(x => x.CreatorPKID, map => map.NotNullable(false));
        Property(x => x.Locked, map => {

            map.NotNullable(true);

            map.Type(NHibernateUtil.Boolean);

        });
        Property(x => x.RatingGroupID);
        Property(x => x.CDate);

        Property(x => x.ContentType, map => map.NotNullable(true));

        Property(x => x.ContentRef);
        Property(x => x.CreatorSpecialName);

        Bag(x => x.Texts, mapping =>
        {
            mapping.Lazy(CollectionLazy.NoLazy);
            mapping.Key(k =>
            {
                k.Column("ContentId");
                
            });
            mapping.Inverse(true);
            mapping.Cascade(Cascade.All);
            
            mapping.Fetch(CollectionFetchMode.Select);
           
        },
           r => r.OneToMany());

        ManyToOne(x => x.User, m =>
        {

            m.Column(c => c.Name("CreatorPKID"));
            m.Fetch(FetchKind.Select);
            m.Cascade(Cascade.None);
            m.Insert(false);
            m.Update(false);
            m.Lazy(LazyRelation.NoLazy);

        });

        ManyToOne(x => x.Category, m =>
        {

            m.Column(c => c.Name("CID"));
            m.Fetch(FetchKind.Select);
            m.Cascade(Cascade.None);
            m.Insert(false);
            m.Update(false);
            m.Lazy(LazyRelation.NoLazy);

        });

        //OneToOne(x => x.Category, map =>
        //{
        //    map.PropertyReference(
        //    map.Column("CID");
        //    map.NotFound(NotFoundMode.Ignore);
        //    map.Cascade(Cascade.None);
        //});

        //ManyToOne(x => x.User, map =>
        //{
        //    map.PropertyRef("PKID");
        //    map.Column("CreatorPKID");

        //    map.Cascade(Cascade.None);
        //});
    }

    #endregion Constructors
}
这里是Content_Text表

public class ContentTextMap : ClassMapping<Persistence.Domain.ContentText>
{
    #region Constructors

    public ContentTextMap()
    {
        Table(Ren.CMS.CORE.Config.RenConfig.DB.Prefix +"Content_Text");
        Schema("dbo");
        Lazy(false);
        Id<int>(x => x.Id, map => {

            map.Generator(Generators.Identity);

        });
        Property(x => x.ContentId, map => map.NotNullable(true));
        Property(x => x.LangCode, map => map.NotNullable(true));

        //Property(x => x.Id, map => map.NotNullable(true));
        Property(x => x.Title, map => map.NotNullable(true));

        Property(x => x.Seoname);

        Property(x => x.MetaKeyWords);
        Property(x => x.MetaDescription);
        Property(x => x.PreviewText);

        Property(x => x.LongText, x => x.Type(NHibernateUtil.StringClob));


        //OneToOne(x => x.Category, map =>
        //{
        //    map.PropertyReference(
        //    map.Column("CID");
        //    map.NotFound(NotFoundMode.Ignore);
        //    map.Cascade(Cascade.None);
        //});

        //ManyToOne(x => x.User, map =>
        //{
        //    map.PropertyRef("PKID");
        //    map.Column("CreatorPKID");

        //    map.Cascade(Cascade.None);
        //});
    }

    #endregion Constructors
}
这是从NHibernate执行的查询:

NHibernate:插入dbo.nfcms_Content CDate、Cid、ContentRef、ContentType、creatorchid、CreatorSpecialName、Locked、RatingGroupID值@p0、@p1、@p2、@p3、@p4、@p5、@p6、@p7;选择范围和标识@p0=27.10.2014 13:51:58[Type:DateTime 0],@p1=NULL[Type:Guid 0],@p2=0[Type:Int32 0],@p3='eNews'[Type:String 4000],@p4=NULL[Type:Guid 0],@p5=[Type:String 4000],@p6=False[Type:Boolean 0],@p7=0[Type:Int32 0]

NHibernate:插入dbo.nfcms\u Content\u Text ContentId、LangCode、LongText、, 元描述、元关键字、预览文本、Seoname、标题值@p0、@p1、@p2、@p3、@p4、@p5、@p6、@p7;选择范围和标识@p0=0[Type:Int32 0],@p1='de de'[Type:String 4000],@p2='Test'[Type:String 1073741823],@p3='Test'[Type:String 4000],@p4='Test'[Type:String 4000],@p5='Test'[Type:String 4000],@p6='TEST123455'[Type:String 4000],@p7='Test'[Type:String 4000]

谁能告诉我怎么了?我搜索了又搜索,没有找到解决方案…

因为我们确实有一对多关系端映射为一个具有反向设置的包,这是绝对正确的,如下所示:

public ContentMap()
{
    ...
    Bag(x => x.Texts, mapping =>
    {
        mapping.Lazy(CollectionLazy.NoLazy);
        mapping.Key(k =>
        {
            k.Column("ContentId");

        });
        ...
public class Content
{
   public virtual IList<ContentText> Texts { get; set; }
   ...

public class ContentText
{
   public virtual Content Content { get; set; }
   public virtual int ContentId { get; set; }
   ...
我们需要另一端——多对一。在我们的例子中,它必须由引用内容表示,也可以表示为只读int ContentId值

期待这样的POCO:

public ContentMap()
{
    ...
    Bag(x => x.Texts, mapping =>
    {
        mapping.Lazy(CollectionLazy.NoLazy);
        mapping.Key(k =>
        {
            k.Column("ContentId");

        });
        ...
public class Content
{
   public virtual IList<ContentText> Texts { get; set; }
   ...

public class ContentText
{
   public virtual Content Content { get; set; }
   public virtual int ContentId { get; set; }
   ...

很高兴看到这一点!享受NHibernate,神奇的工具;