Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 亚硝酸铵;删除子项删除父项?_C#_.net_Database_Nhibernate_Fluent Nhibernate - Fatal编程技术网

C# 亚硝酸铵;删除子项删除父项?

C# 亚硝酸铵;删除子项删除父项?,c#,.net,database,nhibernate,fluent-nhibernate,C#,.net,Database,Nhibernate,Fluent Nhibernate,为什么在我删除子项(员工)时要删除父项(存储) 我配置了约定级联。全部 用户输入顺序非常简单: 从空数据库开始 添加父项 保存、加载(加载=重新加载完整对象图形) 添加一个孩子 保存、加载 删除子项 结果:空数据库。(已删除父项) 这可能是一个基本的映射错误,因为这是我第一次使用NHibernate。我希望Store成为聚合根,并认为通过not在Store.Staff属性上设置Inverse,则Store table将负责保存,从而生成聚合根。这是一种误解吗?事实上,如果我使用逆或不使用逆,

为什么在我删除子项(员工)时要删除父项(存储)

我配置了约定级联。全部

用户输入顺序非常简单:

  • 从空数据库开始
  • 添加父项
  • 保存、加载(加载=重新加载完整对象图形)
  • 添加一个孩子
  • 保存、加载
  • 删除子项
  • 结果:空数据库。(已删除父项)
这可能是一个基本的映射错误,因为这是我第一次使用NHibernate。我希望Store成为聚合根,并认为通过not在Store.Staff属性上设置Inverse,则Store table将负责保存,从而生成聚合根。这是一种误解吗?事实上,如果我使用逆或不使用逆,我仍然得到相同的结果。所以也许 这不是问题所在,但我也想理解这一点

不使用更广泛的会话范围是经过深思熟虑的,因为我想学习如何使用分离的和暂时的实体

员工删除方法:

class EmployeeRepository
        public static void Delete(Employee employee)
        {
            using (ISession session = FNH_Manager.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    if (employee.Id != 0)
                    {
                      var emp =  session.Get(typeof(Employee), employee.Id);

                      if (emp != null)
                      {
                        session.Delete(emp);
                        transaction.Commit();
                      }
                    }
                }
            }
        } 
   public class Store
    {
        public int Id { get; private set; }
        public string Name { get; set; }
        public IList<Product> Products { get; set; }
        public IList<Employee> Staff { get; set; }

        public Store()
        {
            Products = new List<Product>();
            Staff = new List<Employee>();
        }


        // AddProduct & AddEmployee is required. "NH needs you to set both sides before
        // it will save correctly" ??

        public void AddProduct(Product product)
        {
            product.StoresStockedIn.Add(this);
            Products.Add(product);
        }

        public void AddEmployee(Employee employee)
        {
            employee.Store = this;
            Staff.Add(employee);
        }
    }

   public class Employee
    {
        public int Id { get;  private set; }
        public string FirstName { get;  set; }
        public string LastName { get;  set; }
        public Store Store { get; set; }
    }
映射

public class StoreMap : ClassMap<Store>
{
    public StoreMap()
    {
        Id(x => x.Id);
        Map(x =>  x.Name);
        HasMany(x => x.Staff)    // 1:m
            .Inverse()    // tried both with and without, what is correct?
            .Cascade.All();       
        HasManyToMany(x => x.Products)  // m:m
            .Cascade.All()
            .Table("StoreProduct");    
    }
}

public class EmployeeMap : ClassMap<Employee> 
{

    public EmployeeMap()
    {
        Id(x => x.Id);                // By default an int Id is generated as identity
        Map(x => x.FirstName);
        Map(x => x.LastName);
        References(x => x.Store);    // m:1
    }
}

public class ProductMap : ClassMap<Product>
{
    public ProductMap() 
    {
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Name).Length(20);
        Map(x => x.Price).CustomSqlType("decimal").Precision(9).Scale(2);
        HasManyToMany(x => x.StoresStockedIn)
        .Cascade.All()
        .Inverse()
        .Table("StoreProduct");
     }

}
公共类存储映射:类映射
{
公共存储地图()
{
Id(x=>x.Id);
Map(x=>x.Name);
HasMany(x=>x.Staff)//1:m
.Inverse()//尝试了带和不带,什么是正确的?
.Cascade.All();
HasManyToMany(x=>x.Products)//m:m
.Cascade.All()
.表格(“存储产品”);
}
}
公共类EmployeeMap:ClassMap
{
公共雇员地图()
{
Id(x=>x.Id);//默认情况下,生成一个int-Id作为标识
Map(x=>x.FirstName);
Map(x=>x.LastName);
引用(x=>x.Store);//m:1
}
}
公共类ProductMap:ClassMap
{
公共产品地图()
{
Id(x=>x.Id).GeneratedBy.Identity();
Map(x=>x.Name).Length(20);
地图(x=>x.Price).CustomSqlType(“十进制”).Precision(9).Scale(2);
HasManyToMany(x=>x.StoresStockedIn)
.Cascade.All()
.Inverse()
.表格(“存储产品”);
}
}
实体:

class EmployeeRepository
        public static void Delete(Employee employee)
        {
            using (ISession session = FNH_Manager.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    if (employee.Id != 0)
                    {
                      var emp =  session.Get(typeof(Employee), employee.Id);

                      if (emp != null)
                      {
                        session.Delete(emp);
                        transaction.Commit();
                      }
                    }
                }
            }
        } 
   public class Store
    {
        public int Id { get; private set; }
        public string Name { get; set; }
        public IList<Product> Products { get; set; }
        public IList<Employee> Staff { get; set; }

        public Store()
        {
            Products = new List<Product>();
            Staff = new List<Employee>();
        }


        // AddProduct & AddEmployee is required. "NH needs you to set both sides before
        // it will save correctly" ??

        public void AddProduct(Product product)
        {
            product.StoresStockedIn.Add(this);
            Products.Add(product);
        }

        public void AddEmployee(Employee employee)
        {
            employee.Store = this;
            Staff.Add(employee);
        }
    }

   public class Employee
    {
        public int Id { get;  private set; }
        public string FirstName { get;  set; }
        public string LastName { get;  set; }
        public Store Store { get; set; }
    }
公共类存储
{
public int Id{get;private set;}
公共字符串名称{get;set;}
公共IList产品{get;set;}
公共IList职员{get;set;}
公共商店()
{
产品=新列表();
职员=新名单();
}
//AddProduct和AddEmployee是必需的。“NH需要您在
//它将正确保存“”??
公共产品(产品)
{
product.StoresStockedIn.Add(此);
产品。添加(产品);
}
公共无效添加员工(员工)
{
employee.Store=this;
Staff.Add(雇员);
}
}
公营雇员
{
public int Id{get;private set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共存储{get;set;}
}
程序伪代码和生成的“SQL”:

程序启动

Load:Stores-Stores=StoreRepository.GetAll()

NHibernate:从[Store]中选择此Id作为Id3\u 0\u,此名称作为Name3\u 0\u_ 添加父项:将存储添加到空集合存储

保存:StoreRepository.SaveOrUpdate(存储)

NHibernate:从[Store]store0中选择store0.Id作为Id3\u 0\u,store0\u.Name作为Name3\u 0\u,其中store0\u.Id=@p0@p0=0[类型:Int32(0)] NHibernate:插入[Store](名称)值(@p0);选择SCOPE_IDENTITY()@p0=NULL[类型:字符串(4000)] Load:stores=StoreRepository.GetAll()

NHibernate:从[Store]中选择此Id作为Id3\u 0\u,此名称作为Name3\u 0\u_ NHibernate:选择products0.Store\u id作为Store2\u 1\u,products0.Product\u id作为Product1\u 1\u,Product1.id作为Id1\u 0\u,Product1.Name作为Name1\u 0\u,Product1.Price作为Price 1\u 0\u来自StoreProducts0产品0的左外连接[Product]Product1\u在products0上。Product\u id=Product1\u.id,其中products0.Store\u id=@p0@p0=16[类型:Int32(0)] NHibernate:选择staff0_uu.Store_uid作为Store4_u1_uu,staff0_u.id作为Id1_uu,staff0_u.id作为Id0_u,staff0.FirstName作为FirstName0_u,staff0.LastName作为LastName0_u,staff0_u.Store_uid作为Store4_0_u从[Employee]staff0_0,其中staff0.Store_uid=@p0@p0=16[类型:Int32(0)] 添加子项:为所选存储添加空子项集合

保存:StoreRepository.SaveOrUpdate(存储)

NHibernate:从[Store]store0中选择store0.Id作为Id3\u 0\u,store0\u.Name作为Name3\u 0\u,其中store0\u.Id=@p0@p0=16[类型:Int32(0)] NHibernate:选择products0.Store\u id作为Store2\u 1\u,products0.Product\u id作为Product1\u 1\u,Product1.id作为Id1\u 0\u,Product1.Name作为Name1\u 0\u,Product1.Price作为Price 1\u 0\u来自StoreProducts0产品0的左外连接[Product]Product1\u在products0上。Product\u id=Product1\u.id,其中products0.Store\u id=@p0@p0=16[类型:Int32(0)] NHibernate:选择staff0_uu.Store_uid作为Store4_u1_uu,staff0_u.id作为Id1_uu,staff0_u.id作为Id0_u,staff0.FirstName作为FirstName0_u,staff0.LastName作为LastName0_u,staff0_u.Store_uid作为Store4_0_u从[Employee]staff0_0,其中staff0.Store_uid=@p0@p0=16[类型:Int32(0)] NHibernate:在[Employee](FirstName、LastName、Store_id)中插入值(@p0、@p1、@p2);选择SCOPE_IDENTITY()@p0=NULL[类型:字符串(4000)],@p1=NULL[类型:字符串(4000)],@p2=16[类型:Int32(0)] Load:stores=StoreRepository.GetAll()

NHibernate:从[Store]中选择此Id作为Id3\u 0\u,此名称作为Name3\u 0\u_ NHibernate:选择products0.Store\u id作为Store2\u 1\u,products0.Product\u id作为Product1\u 1\u,Product1.id作为Id1\u 0\u,Product1.Name作为Name1\u 0\u,Product1.Price作为Price 1\u 0\u来自StoreProducts0产品0的左外连接[Product]Product1\u在products0上。Product\u id=Product1\u.id,其中products0.Store\u id=@p0@p0=16[类型: NHibernate: SELECT this_.Id as Id3_0_, this_.Name as Name3_0_ FROM [Store] this_ NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT store0_.Id as Id3_0_, store0_.Name as Name3_0_ FROM [Store] store0_ WHERE store0_.Id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: INSERT INTO [Employee] (FirstName, LastName, Store_id) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = NULL [Type: String (4000)], @p2 = 16 [Type: Int32 (0)] NHibernate: SELECT this_.Id as Id3_0_, this_.Name as Name3_0_ FROM [Store] this_ NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT employee0_.Id as Id0_1_, employee0_.FirstName as FirstName0_1_, employee0_.LastName as LastName0_1_, employee0_.Store_id as Store4_0_1_, store1_.Id as Id3_0_, store1_.Name as Name3_0_ FROM [Employee] employee0_ left outer join [Store] store1_ on employee0_.Store_id=store1_.Id WHERE employee0_.Id=@p0;@p0 = 35 [Type: Int32 (0)] NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 16 [Type: Int32 (0)] NHibernate: DELETE FROM [Employee] WHERE Id = @p0;@p0 = 35 [Type: Int32 (0)] NHibernate: DELETE FROM [Store] WHERE Id = @p0;@p0 = 16 [Type: Int32 (0)] NHibernate: SELECT this_.Id as Id3_0_, this_.Name as Name3_0_ FROM [Store] this_ NHibernate: SELECT this_.Id as Id3_0_, this_.Name as Name3_0_ FROM [Store] this_ NHibernate: SELECT store0_.Id as Id3_0_, store0_.Name as Name3_0_ FROM [Store] store0_ WHERE store0_.Id=@p0;@p0 = 0 [Type: Int32 (0)] NHibernate: INSERT INTO [Store] (Name) VALUES (@p0); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)] NHibernate: SELECT this_.Id as Id3_0_, this_.Name as Name3_0_ FROM [Store] this_ NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT store0_.Id as Id3_0_, store0_.Name as Name3_0_ FROM [Store] store0_ WHERE store0_.Id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: INSERT INTO [Employee] (FirstName, LastName, Store_id) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY();@p0 = NULL [Type: String (4000)], @p1 = NULL [Type: String (4000)], @p2 = 1 [Type: Int32 (0)] NHibernate: UPDATE [Employee] SET Store_id = @p0 WHERE Id = @p1;@p0 = 1 [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)] NHibernate: SELECT this_.Id as Id3_0_, this_.Name as Name3_0_ FROM [Store] this_ NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT employee0_.Id as Id0_1_, employee0_.FirstName as FirstName0_1_, employee0_.LastName as LastName0_1_, employee0_.Store_id as Store4_0_1_, store1_.Id as Id3_0_, store1_.Name as Name3_0_ FROM [Employee] employee0_ left outer join [Store] store1_ on employee0_.Store_id=store1_.Id WHERE employee0_.Id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT products0_.Store_id as Store2_1_, products0_.Product_id as Product1_1_, product1_.Id as Id1_0_, product1_.Name as Name1_0_, product1_.Price as Price1_0_ FROM StoreProduct products0_ left outer join [Product] product1_ on products0_.Product_id=product1_.Id WHERE products0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id as Id0_0_, staff0_.FirstName as FirstName0_0_, staff0_.LastName as LastName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_ WHERE staff0_.Store_id=@p0;@p0 = 1 [Type: Int32 (0)] NHibernate: UPDATE [Employee] SET Store_id = null WHERE Store_id = @p0;@p0 = 1 [Type: Int32 (0)] NHibernate: DELETE FROM [Employee] WHERE Id = @p0;@p0 = 1 [Type: Int32 (0)] NHibernate: DELETE FROM [Store] WHERE Id = @p0;@p0 = 1 [Type: Int32 (0)] NHibernate: SELECT this_.Id as Id3_0_, this_.Name as Name3_0_ FROM [Store] this_
    private static ISessionFactory CreateSessionFactory()
    {
        if (sessionFactory == null)
        {                              
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(Properties.Settings.Default.FnhDbString)
                .Cache(c => c
                    .UseQueryCache()).ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<EmployeeMap>()  
                .Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())
                .Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultCascade.All())
                .ExportTo("D:/VB/"))              
                .ExposeConfiguration(c => cfg = c)
                .BuildSessionFactory();
        }
        return sessionFactory; 
    }
// For all alternatives, configuration does not specify cascade-convention. // HasMany(x => x.Staff); // 1. add store, save, load, add employee, // save: TransientObjectException; Employee HasMany(x => x.Staff).Inverse(); // 2. As 1 // HasMany(x => x.Staff).Cascade.All(); // 3. Add store, Save, Load, Add Employee, Save, Load, // Delete Employee: ObjectDeletedException // HasMany(x => x.Staff).Inverse().Cascade.All(); // 4. As 3 // HasMany(x => x.Staff).Inverse().Cascade.AllDeleteOrphan(); // 5. As 3/4 // HasMany(x => x.Staff).Cascade.None(); // 6. As 1/2 // Exception of 1) // On StoreRepositorySaveOrUpdate(stores): TransientObjectException: // object references an unsaved transient instance - save the transient instance before flushing. // Type: FNHib_Test.Entities.Employee, Entity: FNHib_Test.Entities.Employee // Exception of 3) // On EmployeeRepository.Delete(employee); transaction.Commit() // ObjectDeletedException was unhandled: // deleted object would be re-saved by cascade // (remove deleted object from associations)[FNHib_Test.Entities.Employee#1]
    public static void Delete(Employee employee)
    {
        using (ISession session = FNH_Manager.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                employee.Store = null;
                if (employee.Id != 0) 
                { 
                  // var emp =  session.Get(typeof(Employee), employee.Id);
                  Employee emp = session.Get<Employee>( employee.Id);
                  if (emp != null)
                  {
                    emp.Store = null;
                    session.Delete(emp);
                    transaction.Commit();
                  } 
                }
            }
        }
    } 
                  if (emp != null)
                  {
                    emp.Store = null;
                    session.Delete(emp);
                    transaction.Commit();
                  }
    public static void Delete(Employee employee)
    {
        using (ISession session = FNH_Manager.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {

                if (employee.Id != 0) 
                { 
                  Employee emp = session.Get<Employee>(employee.Id);

                  if (emp != null)
                  {
                    emp.Store.Staff.Remove(emp);
                    session.Delete(emp);
                    transaction.Commit();
                  } 
                }
            }
        }
    }