1

给定以下模型,我希望能够输入一个新的WriteOffApprovalUser并让该Employee字段为空。这是 1:1 或空关系。

public class WriteOffApprovalUser
{
    public virtual string UserName { get; set; }
    public virtual Employee Employee { get; set; }
}

public class Employee
{
    public virtual string EmployeeID { get; set; }
    public virtual string EmployeeStatusCode { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string JobTitle { get; set; }
    public virtual string Division { get; set; }
    public virtual string Department { get; set; }
    public virtual string Location { get; set; }
    public virtual string City { get; set; }
    public virtual string DeskLocation { get; set; }
    public virtual string MailID { get; set; }
    public virtual string Phone { get; set; }
    public virtual string PreferredName { get; set; }
    public virtual string Fax { get; set; }
    public virtual string SecCode { get; set; }
    public virtual string SupervisorID { get; set; }
    public virtual string UserId { get; set; }
}

类图

public class WriteOffApprovalUserMap : ClassMap<WriteOffApprovalUser>
{
    public WriteOffApprovalUserMap()
    {
        Table("WRITEOFF_APPROVAL_USER");

        Id(x => x.UserName).Column("USER_NAME");

        //Map(x => x.Employee).Nullable();

        HasOne(x => x.Employee)
            .Class<Employee>()
            .Constrained()
            .Cascade.SaveUpdate()
            .PropertyRef("UserId");
    }
}

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Table("ADP_EMPLOYEE");

        Id(x => x.EmployeeID).Column("EMPLID").GeneratedBy.Native("");

        Map(x => x.FirstName).Column("FIRST_NAME");
        Map(x => x.LastName).Column("LAST_NAME");
        Map(x => x.PreferredName).Column("PREFERRED_NAME");
        Map(x => x.UserId).Column("USER_ID");
    }
}

查询/保存

using (var session = SessionProvider.GetSession())
        {
            using (var tx = session.BeginTransaction())
            {
                var user = new WriteOffApprovalUser() { UserName = "SAMSTR" };

                session.Save(user);

                tx.Commit();
            }
        }

这抱怨那Employee是空的。如何指定 Employee 可以为空?

另外,所有Id字段都必须是完整的吗?我们桌子上的很多键都是字符串。

4

1 回答 1

-1

首先,如果您将其设置为 1:1,则它不应为 NULL,因为它不是正确的设计。

但这里有一个如何做到这一点的例子:

1) 一种方式

HasOne(x => x.Employee)
            .Class<Employee>().Nullable().NotFound.Ignore().PropertyRef("UserId");

2) 第二种方式

References(x => x.Category).Column("UserId").Nullable().NotFound.Ignore();
于 2013-04-10T15:30:04.917 回答