- .NET Core SDK (соответствует любому файлу global.json): версия 2.2.300; Зафиксировать: 73efd5bd87
- NHibernate 5.2.5
Код: Выделить всё
public class Customer : Entity {
public Customer() {
Initialize();
}
public virtual string Name { get; set; }
public virtual string LegalName { get; set; }
public virtual string VATCode { get; set; }
public virtual ICollection Sites { get; set; }
public virtual DateTime Created { get; set; } = DateTime.UtcNow;
private void Initialize() {
Sites = new List();
}
}
public class Site : Entity {
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string Country { get; set; }
public virtual Customer Customer { get; set; }
}
Код: Выделить всё
internal class CustomerConfiguration : ClassMapping {
public CustomerConfiguration() {
Table( TableNames.CustomersTable );
Id( x => x.EntityID, im => {
im.Column( "CustomerID" );
im.Generator( Generators.Identity );
} );
[... code omitted for brevity ...]
Set( property => property.Sites,
collection => {
collection.Fetch( CollectionFetchMode.Join );
collection.Lazy( CollectionLazy.Lazy );
collection.Cascade( Cascade.Persist.Include( Cascade.DeleteOrphans ) );
collection.Inverse( true );
collection.Key( keyMapping => {
keyMapping.Column( "CustomerID" );
} );
},
mapping => {
mapping.OneToMany( relationalMapping => {
relationalMapping.Class( typeof( Site ) );
} );
} );
}
}
internal class SiteConfiguration : ClassMapping {
public SiteConfiguration() {
Table( TableNames.SitesTable );
Id( x => x.EntityID, im => {
im.Column( "SiteID" );
im.Generator( Generators.Identity );
} );
[... code omitted for brevity ...]
ManyToOne( x => x.Customer, mm => {
mm.Column( "CustomerID" );
mm.NotNullable( true );
} );
}
}
Код: Выделить всё
using ( var session = sessionFactory.OpenSession() ) {
var customer = new Customer() {
Name = $"Customer 1",
LegalName = $"Customer 1 LLC",
VATCode = "xxxxxxxxx",
Created = DateTime.UtcNow
};
customer.Sites.Add( new Site() {
Address = $"Address Customer 1",
City = $"City Customer 1",
Country = $"Country Customer 1",
Customer = customer
} );
session.Save( customer );
}
Необработанное исключение: NHibernate.TransientObjectException: объект ссылается на несохраненный временный экземпляр — сохраните временный экземпляр перед очисткой или установите для свойства каскадное действие, которое приведет к его автосохранению. Тип: Nhibernate.ConsoleApp.Entities.Site, Entity: [Сайт 0]
Есть предложения?
РЕДАКТИРОВАТЬ
На самом деле проблема была в другой области. это произошло из-за наличия двух зарегистрированных Слушателей (
Код: Выделить всё
IDeleteEventListenerПодробнее здесь: https://stackoverflow.com/questions/569 ... eing-saved
Мобильная версия