我正在嘗試制作一張附有 2 個一對多關聯商店的表格。
經過一些嘗試,我修復了我可以修復的問題,但我被困在了那個錯誤中。
無法映射屬性“屬性名稱”,因為它屬于“物件”型別,不是受支持的原始型別或有效的物體型別。
fluentAPI 的 DbContext 類
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Transaction> Transactions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Transaction>()
.HasOne(p => p.Receiver)
.WithMany(t => t.ReceiveTransactions)
.HasForeignKey(m => m.ReceiverID)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Transaction>()
.HasOne(p => p.Sender)
.WithMany(t => t.SendTransactions)
.HasForeignKey(m => m.SenderID)
.OnDelete(DeleteBehavior.Restrict);
}
}
這是其余的課程
用戶類
public class ApplicationUser : IdentityUser
{
public ICollection<Transaction> SendTransactions { get; set; }
public ICollection<Transaction> ReceiveTransactions { get; set; }
public Account Account { get; set; }
public ApplicationUser()
{
}
}
交易
public class Transaction
{
[Key]
public String TransactionID { get; set; }
public String SenderID { get; set; }
public String ReceiverID { get; set; }
[DataType(DataType.DateTime)]
public DateTime Date { get; set; }
public String Currency { get; set; }
public float Amount { get; set; }
[ForeignKey("SenderID")]
public ApplicationUser Sender { get; set; }
[ForeignKey("ReceiverID")]
public ApplicationUser Receiver { get; set; }
public object ReceiverId { get; internal set; }
public Transaction(String TransactionID, String SenderID, String ReceiverID, DateTime Date, String Currency, float Amount)
{
this.TransactionID = TransactionID;
this.SenderID = SenderID;
this.ReceiverID = ReceiverID;
this.Date = Date;
this.Currency = Currency;
this.Amount = Amount;
}
}
uj5u.com熱心網友回復:
public object ReceiverId { get; internal set; }
此屬性導致錯誤,因為它不是物體框架識別用于映射的型別。將資料型別更改為 int 或 string 等原始資料型別,或洗掉該屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415870.html
標籤:
上一篇:如何在控制器中進行無休止的會話
