2

我有以下表格:

  • LANDLORD = Id(主键)、FirstName、Surname、EmailAddress、Title;
  • PROPERTY = Id(主键)、Type、NumberOfBedrooms、Address1、Address2、City、County、PostCode、LandlordId(Landlord 实体的外键);

My Domain 类是:

public class Landlord:BaseEntity
{
    public virtual string Title { get; set; }
    public virtual string Surname { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string EmailAddress { get; set; }
    public virtual IList<Property> Properties { get; set; }

    public Landlord()
    {
        Properties = new List<Property>();
    }
}

public class Property:BaseEntity
{
    public virtual string Type { get; set; }
    public virtual int NumberOfBedrooms { get; set;}
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string City { get; set; }
    public virtual string County { get; set; }
    public virtual string PostCode { get; set; }
    public virtual Landlord Landlord { get; set; }

}

我流利的 ​​NHibernate 地图是:

public sealed class LandlordMap:ClassMap<Landlord>
{
    public LandlordMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Title);
        Map(x => x.Surname);
        Map(x => x.FirstName);
        Map(x => x.EmailAddress);
        HasMany(x => x.Properties)
            .KeyColumns.Add("LandlordId")
            .Inverse()
            .Cascade.All();
        Table("LANDLORD");
    }
}

public sealed class PropertyMap:ClassMap<Property>
{
    public PropertyMap()
    {
        Id(x => x.Id).GeneratedBy.Identity(); 
        Map(x => x.Type);
        Map(x => x.NumberOfBedrooms);
        Map(x => x.Address1);
        Map(x => x.Address2);
        Map(x => x.City);
        Map(x => x.County);
        Map(x => x.PostCode);
        References(x => x.Landlord, "LandlordId");
        Table("PROPERTY");
    }
}

为了测试一个房东是否保存到数据库中,我有以下代码:

public class Program
{
    static void Main(string[] args)
    {
        ILandlordRepository rep = new LandlordRepository();

        //Create property object
        Property p1 = new Property
        {
            Address1 = "123 LA Road",
            Address2 = "Bilston",
            City = "Harlem",
            County = "West Mids",
            NumberOfBedrooms = 2,
            PostCode = "wv134wd",
            Type = "Bungalow"
        };

        //Create landlord and assign property
        var landlord = new Landlord();
        landlord.Title = "Dr";
        landlord.FirstName = "Rohit";
        landlord.Surname = "Kumar";
        landlord.EmailAddress = "rkhkp@p.com";
        landlord.Properties.Add(p1);

        //Add to the database
        rep.SaveOrUpdate(landlord);


        Console.ReadKey();

    }
}

当我调用 SaveOrUpdate 时,我收到此错误:

无法插入:[Homes4U.Service.DomainClasses.Property][SQL: INSERT INTO PROPERTY (Type, NumberOfBedrooms, Address1, Address2, City, County, PostCode, LandlordId) VALUES (?, ?, ?, ?, ?, ?, ?, ?); 选择 SCOPE_IDENTITY()]

现在有人为什么会这样吗?

4

1 回答 1

0

在您的映射中,您已指定 Property 对象负责保存其对 Landlord 的引用。

HasMany(x => x.Properties)
            .KeyColumns.Add("LandlordId")
             // Inverse means that the other side of the 
             // relationship is responsible for creating the reference
            .Inverse()
            .Cascade.All();

当您尝试保存对象时,Properties 集合中的 Property 对象没有引用它所属的房东。在后台,它会尝试在 LandlordId 列中插入 NULL。

这就是为什么你得到你的错误。

编辑

要解决,先救房东,不提及任何财产。

    ILandlordRepository rep = new LandlordRepository();
    IPropertyRepository pro = new PropertyRepository();

    //Create landlord
    var landlord = new Landlord();
    landlord.Title = "Dr";
    landlord.FirstName = "Rohit";
    landlord.Surname = "Kumar";
    landlord.EmailAddress = "rkhkp@p.com";

    rep.SaveOrUpdate(landlord);

    // Now add the landlord reference to the property and
    // save
    /Create property object
    Property p1 = new Property
    {
        Address1 = "123 LA Road",
        Address2 = "Bilston",
        City = "Harlem",
        County = "West Mids",
        NumberOfBedrooms = 2,
        PostCode = "wv134wd",
        Type = "Bungalow",
        Landlord = landlord
    };

    pro.SaveOrUpdate(p1);

保存后您可能需要重新加载房东。

编辑 2

看到这个关于逆的问题。

NHibernate 中的逆属性

于 2012-10-11T09:08:02.920 回答