我有以下表格:
我的域类是:
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; }
}
我的Fluent 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()]
现在有人为什么会这样吗?
答案 0 :(得分: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
在Inverse上查看此问题。