【发布时间】:2021-07-04 13:47:53
【问题描述】:
我正在尝试制作一张附有 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;
}
}
【问题讨论】:
-
public object ReceiverId您可能想删除此属性
标签: c# sql asp.net-mvc-3 entity-framework-core