2

私は次のテーブルを持っています:

  • LANDLORD = Id(主キー)、FirstName、Surname、EmailAddress、Title;
  • PROPERTY = Id(主キー)、Type、NumberOf Bedrooms、Address1、Address2、City、County、PostCode、LandlordId(Landlordエンティティへの外部キー);

私のドメインクラスは次のとおりです。

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; }

}

私のFluentNHibernateマップは次のとおりです。

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、NumberOf Bedrooms、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);

保存後、Landlord をリロードする必要がある場合があります。

編集2

Inverse でこの質問を参照してください。

Hibernate の逆属性

于 2012-10-11T09:08:02.920 に答える