我使用 .netcore 5,代碼優先,我有一個表:
public class PaymentEntity
{
public int ID { get; set; } = default!;
public string? QID { get; set; } = default!;
public string? Status { get; set; } = default!;
public int? PlatformId { get; set; } = default!;
public int? CustomerId { get; set; } = default!;
public string? UpdatedAmount { get; internal set; } = default!;
}
這是我標記為客戶的第二個表:
public class FlaggedCustomer
{
public int Id { get; set; }
public int? CustomerId { get; set; }
public string? Comments { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset Modified { get; set; }
public PaymentEntity? Paymententity { get; set; }
}
每次新的付款到來時,如果某些條件不滿足,我需要標記客戶并將其存盤在標記的客戶表中,這是我的存盤方式:
var newFlaggedCustomer= new FlaggedCustomer()
{
CustomerId = newCustomer.CustomerId,
Created = DateTimeOffset.UtcNow,
Modified = DateTimeOffset.UtcNow,
Comments = "some comments "
};
dbContext.FlaggedCustomer.Add(newFlaggedCustomer);
dbContext.SaveChanges();
客戶可以有很多付款,但標記的客戶中只能有一個客戶 ID,我的意思是,如果您的客戶 ID 為 2,您可以在付款物體中找到客戶 ID 2 的許多付款,但如果他被標記,則只有一個客戶 ID 2 可以是在標記的客戶上找到,問題是客戶被標記,但在 db(FlaggedCustomer) 表中,PaymentHistory 欄位為空,當我使用 Include 導航時,回傳空,我想查看付款歷史記錄中的哪筆付款使客戶標記..任何幫助將不勝感激
uj5u.com熱心網友回復:
我不知道如何保存有關FlaggedCustomerand的資料PaymentEntity,但在我看來,每當生成付款時,它都會被插入到PaymentEntity表中,然后您需要檢查是否FlaggedCustomer應該插入新專案或只是更新。所以應該有一些類似于這樣的代碼:
//insert payment entity anyway
_context.Add<PaymentEntity>(payment);
//check and insert or update FlaggedCustomer
if (_context.Customers.Any(e => e.Id == cus.Id))
{
var customer = _context.Customers.First(a => a.Id == cus.Id);
customer.Modified = DateTimeOffset.UtcNow;
}
else
{
_context.Add<FlaggedCustomer>(cus);
}
_context.SaveChanges();
那么在查詢支付資料查看支付歷史時,我想我們需要使用include如果你在join這里設定外鍵或查詢,可能類似于:
var queryResult = from flag_customer in _context.Customers
join payment_entity in _context.Payment on flag_customer.CustomerId equals payment_entity.CustomerId
select new
{
CustomerId = flag_customer.CustomerId,
CreateTime = flag_customer.Created,
ModifiedTime = flag_customer.Modified,
PaymentStatus = payment_entity.Status,
PaymentAmount = payment_entity.UpdatedAmount
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371315.html
上一篇:.NET核心中的IOC
下一篇:.net5剃刀頁面路由
