c# - 映射JoinedSubclassMapping

标签 c# hibernate sqlite nhibernate

当我尝试插入 PurchaseCredit 时出现此错误:

SQL逻辑错误或缺少数据库 外键不匹配 - “PurchaseCredit”引用“Deposit”

这是生成的 SQL 的一部分:

create table FinancialDeposit 
(
    Id  integer primary key autoincrement,
    Version BIGINT not null,
    DatePayment date,
    Total money,
    Status INT,
)

create table Deposit 
(
    FinancialDepositId BIGINT not null unique,
    AccountId BIGINT not null,
    PersonName varchar(250) not null,
    primary key (FinancialDepositId),
    constraint FK737CDD9090887321 foreign key (AccountId) references Account,
    constraint FinancialDepositFK foreign key (FinancialDepositId) references FinancialDeposit
)

create table PurchaseCredit 
(
    Id  integer primary key autoincrement,
    Version BIGINT not null,
    PurchaseCode UNIQUEIDENTIFIER not null unique,
    DepositId BIGINT,
    Total money,
    constraint FK737CDB4BFA0E8716 foreign key (DepositId) references Deposit
)

这是 C# 代码的一部分:

public class FinancialDepositMap : ClassMapping<FinancialDeposit>
{
    public FinancialDepositMap()
    {
        Property(x => x.DatePayment, map => map.Column(a => a.SqlType("date")));
        Property(x => x.Total);
        Property(x => x.Status);
        Property(x => x.DatePayment);
        Property(x => x.Hash);
    }
}

public class DepositMap : JoinedSubclassMapping<Deposit>
{
    public DepositMap()
    {
        ManyToOne(x => x.AccountId, m => m.NotNullable(true));
        Property(x => x.PersonName, m => m.NotNullable(true));
    }
}

public class PurchaseCreditMap : ClassMapping<PurchaseCredit>
{
    public PurchaseCreditMap()
    {
        Property(x => x.PurchaseCode, map =>
        {
            map.Unique(true);
            map.NotNullable(true);
            map.Update(false);
        });

        ManyToOne(x => x.Deposit);
        Property(x => x.Total);
    }
}

我如何映射“PurchaseCreditMap”中的“Deposit”列以获取此 SQL:

create table PurchaseCredit 
(
    ...
    constraint FK737CDB4BFA0E8716 foreign key (DepositId) references Deposit (FinancialDepositId)
)

最佳答案

您的映射中没有定义任何键或 ID。

参见 here用于按代码映射中的继承映射

关于c# - 映射JoinedSubclassMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748943/

相关文章:

sqlite - 将静脉数据保存在SQLite数据库中

python - 创建 super 用户 django.db.utils.IntegrityError : NOT NULL constraint failed

c# - 为什么我的 Xamarin.Forms 应用程序在从 Web API 将数据插入 SQLite 数据库后崩溃?

c# - 为什么使用反射无法使用继承接口(interface)的成员?

c# - Unity和ASP.NET WebForms-没有为此对象定义无参数构造函数

java - Hibernate 返回另一个对象

java - Hibernate saveOrUpdate 不更新

java - 测试 DAO 时出错 : sun. reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class

c# - 更改默认的 ASP MVC 请求 header 以添加您自己的值

c# - 将以空字符结尾的字符串列表从外部函数返回到 .NET