简体   繁体   English

带有外键的Fluent nHibernate“非null属性引用了null或瞬态值”

[英]Fluent nHibernate “not-null property references a null or transient value” with Foreign Key

I'm trying to do an insert into a database. 我正在尝试插入数据库。 I have the following two entities: 我有以下两个实体:

USER: 用户:

public class User
{
    private UserDescription _userDescription;

    public virtual uint Id { get; set; }
    public virtual string Email { get; set; }
    public virtual string Username { get; set; }

    public virtual UserDescription UserDescription
    {
        get { return _userDescription; }
        set { _userDescription = value; }
    }

    public virtual void Add(UserDescription userDescription)
    {
        if (_userDescription != null)
        {
            _userDescription.User = null;
        }
        _userDescription = userDescription;
        _userDescription.User = this;
    }

    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("users");

            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Email).Not.Nullable();
            Map(x => x.Username).Not.Nullable();

            HasOne(x => x.UserDescription).Cascade.All().LazyLoad();
        }
    }
}

And USERDESCRIPTION : USERDESCRIPTION

public class UserDescription
{
    public virtual uint Id { get; set; }
    public virtual User User { get; set; }
    public virtual uint UserId { get; set; }
    public virtual string Firstname { get; set; }
    public virtual string Lastname { get; set; }

    public class UserDescrptionMap : ClassMap<UserDescription>
    {
        public UserDescrptionMap()
        {
            Table("usersdescription");

            Id(x => x.Id).GeneratedBy.Identity();

            Map(x => x.UserId);
            Map(x => x.Firstname);
            Map(x => x.Lastname);

            HasOne(x => x.User).Constrained().ForeignKey();
        }
    }
}

And my JSON that I pass through looks like the following: 我通过的JSON如下所示:

{
   "Email": "Example3@gmail.com",
   "Username": "Something123",
   "UserDescription": {
       "Firstname": "John",
       "Lastname": "Doe"
   }
}

I tried doing an insert into User hoping that it would cascade down and also insert into the child table UserDescription, but that didn't work. 我尝试对User进行插入,希望它可以向下层叠并插入子表UserDescription中,但这没用。 So instead I insert into user first then insert into UserDescription (not sure if this is ideal). 因此,我先插入用户,然后再插入UserDescription(不确定这是否理想)。

Anyway, I get the following error when I try to insert into UserDescription after I successfully insert to User: 无论如何,当我成功插入到User后尝试插入UserDescription时,出现以下错误:

not-null property references a null or transient value Users.Entities.UserDescription.User not-null属性引用null或瞬态值Users.Entities.UserDescription.User

Any suggestions? 有什么建议么?

In case anyone's interested, I removed the ".Constrained()" in the UserDescription Mapping and that seemed to work. 如果有人感兴趣,我在UserDescription映射中删除了“ .Constrained()”,这似乎可行。 Although I dont know if that is the best solution! 虽然我不知道这是否是最好的解决方案!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 not-null属性引用一个null或瞬态值 - not-null property references a null or transient value Nhibernate的not-null属性引用一个null或瞬态值Tec.Core.Model.Budget.oInvestmentCode - Nhibernate not-null property references a null or transient value Tec.Core.Model.Budget.oInvestmentCode Nhibernate:not-null属性以多对一关系引用null或瞬态值错误 - Nhibernate : not-null property references a null or transient value error in many to one relationship 使用NHibernate删除实体时,“非空属性引用空值或瞬态值” - “not-null property references a null or transient value” when deleting entity using NHibernate NHibernate - not-null属性引用null或transient值 - NHibernate - not-null property reference a null or transient value not-null属性引用的是null或瞬态值-了解Inverse和Cascade - not-null property references a null or transient value - understanding of Inverse and Cascade NHibernate.PropertyValueException:not-null属性引用null或瞬态和相关的单元测试 - NHibernate.PropertyValueException : not-null property references a null or transient and dependent unit tests 我的nhibernate映射不应该解决这个问题吗? 错误:not-null属性引用null或transient值 - Shouldn't my nhibernate mapping take care of this? Error: not-null property reference a null or transient value 非空外键的流畅 nhibernate 映射(HasMany) - fluent nhibernate mapping with not null foreign key (HasMany) 流利的NHibernate:外键不为空的问题 - Fluent NHibernate: foreign key not null problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM