无法映射 属性 'Property Name',因为它的类型 'object' 不是受支持的原始类型或有效的实体类型
The property 'Property Name' could not be mapped, because it is of type 'object' which is not a supported primitive type or a valid entity type
我正在尝试制作一个 table,它有 2 个一对多的关联。
经过一些尝试,我修复了我能修复的问题,但我被那个错误困住了。
无法映射 属性 'Property Name',因为它的类型 'object' 不是受支持的基本类型或有效的实体类型。
FluentAPI 的 DbContext class
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);
}
}
这是 classes
的其余部分
用户Class
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 { get; internal set; }
此 属性 导致错误,因为它不是 entity framework 可识别的映射类型。将数据类型更改为基本类型,如 int 或 string,或者删除 属性.
我正在尝试制作一个 table,它有 2 个一对多的关联。
经过一些尝试,我修复了我能修复的问题,但我被那个错误困住了。
无法映射 属性 'Property Name',因为它的类型 'object' 不是受支持的基本类型或有效的实体类型。
FluentAPI 的 DbContext class
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);
}
}
这是 classes
的其余部分用户Class
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 { get; internal set; }
此 属性 导致错误,因为它不是 entity framework 可识别的映射类型。将数据类型更改为基本类型,如 int 或 string,或者删除 属性.