gpt4 book ai didi

sqlite - NHibernate - 一对多只是不使用 SQLite

转载 作者:行者123 更新时间:2023-12-03 18:24:35 28 4
gpt4 key购买 nike

TL;博士; NHibernate 反向关系适用于 Azure-SQLMSSQL2012 但不适用于 SQLite
描述:
我目前正在对我的 Asp.Net MVC 应用程序进行单元测试,并在 SQLite 上使用 FluentMigrator 设置我的单元测试。

创建数据库后,我设置了一些我需要的基本条目。
其中之一是产品。
一个Product 有很多ProductSuppliers,一个ProductSupplier 有很多ProductSupplierPrices

public class Product
{
public virtual long Id { get; set; }

public virtual string Number { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
//more properties
public virtual IList<ProductSupplier> Suppliers { get; set; }
//more properties
}

public class ProductSupplier
{
public virtual long Id { get; set; }

public virtual Product Product { get; set; }
public virtual Supplier Supplier { get; set; }

public virtual IList<ProductSupplierPrice> Prices { get; set; }
}

public class ProductSupplierPrice : IHaveId
{
public virtual long Id { get; set; }
public virtual ProductSupplier ProductSupplier { get; set; }
public virtual decimal FromAmount { get; set; }
public virtual decimal Price { get; set; }
}

设置:
  • 创建供应商
  • 创建产品
  • 创建产品供应商
  • 创建 ProductSupplierPrice

  • 测试:
    Product product = this.session.Load<Product>((long)1);
    ProductSupplier productSupplier = product.Suppliers.First(); //<-- Suppliers are null; therefore throws an exception

    如果我单独加载它们以检查关系:
    productSupplierPrice.ProductSupplier <--- Correct Supplier

    productSupplier.Prices <-- Null
    productSupplier.Product <-- Product with Id 1

    product.Suppliers <-- Null

    所以在我看来,多对一方向正确地工作,但一对多(反向关系)不起作用。

    问题仅存在于我的单元测试(SQLite)中,应用程序本身在 Azure-SQL 上运行并且工作正常。

    编辑:
    FluentnHibernate 的映射
    public class ProductMap : ClassMap<Product>
    {
    public ProductMap()
    {
    Id(x => x.Id);
    HasMany(x => x.Suppliers).Inverse().Cascade.DeleteOrphan().BatchSize(20);
    //many more mappings
    }
    }

    public ProductSupplierMap()
    {
    Id(x => x.Id);
    References(x => x.Product);
    References(x => x.Supplier);
    Map(x => x.IsMainSupplier);
    Map(x => x.SupplierProductNumber);
    Map(x => x.CopperSurcharge);
    HasMany(x => x.Prices).Inverse().Cascade.DeleteOrphan().BatchSize(20);
    }

    public ProductSupplierPriceMap()
    {
    Id(x => x.Id);
    References(x => x.ProductSupplier);
    Map(x => x.FromAmount);
    Map(x => x.Price);
    }

    Edit2 - 创建数据库条目:
    Product product = new Product()
    {
    Type = ProductType.Purchase,
    Dispatcher = session.Load<Employee>(employeeId),
    Number = "100.10-1000",
    Name = "Testproduct",
    //Lots of Properties
    Suppliers = new List<ProductSupplier>()
    };
    session.SaveOrUpdate(product);

    ProductSupplier productSupplier = new ProductSupplier()
    {
    Product = product,
    Supplier = session.Load<Supplier>((long)1),
    IsMainSupplier = true,
    SupplierProductNumber = "Artikel123456",
    CopperSurcharge = CopperSurchargeType.DEL700,
    Prices = new List<ProductSupplierPrice>()
    };
    session.Save(productSupplier);

    ProductSupplierPrice productSupplierPrice = new ProductSupplierPrice()
    {
    ProductSupplier = productSupplier,
    FromAmount = 1,
    Price = 5
    };
    session.Save(productSupplierPrice);

    编辑 3.1:
    public static ISession InitializeDatabase()
    {
    NHibernateSessionHolder.CreateSessionFactory();
    session = NHibernateSessionHolder.OpenSession();
    CreateBaseEntries(); //Creates Employees, Supplier, Customer etc
    return session;
    }

    最佳答案

    基于Ayende's article您需要清除插入/更新和查询之间的 session :

    session.Clear();

    似乎是 session 管理,我不确定为什么 session 应该是干净的,但是 session 提供的是您的原始实例(与您提供的保存相同,存储在 session 缓存中)而不是延迟加载的代理。
    private long CreatePurchaseOrder()
    {
    session.Clear();

    var product = this.session.Load<Product>((long)1);
    var productSupplier = product.Suppliers.First();
    var productSupplierPrice = productSupplier.Prices.First();
    return 0;
    }

    关于sqlite - NHibernate - 一对多只是不使用 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33666824/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com