I am trying to make a table that has 2 one to many relatishops attached to it.
After some tries I fixed what I could and I am stuck at that error.
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.
DbContext class for fluentAPI
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);
}
}
And here are the rest of the classes
User Class
public class ApplicationUser : IdentityUser
{
public ICollection<Transaction> SendTransactions { get; set; }
public ICollection<Transaction> ReceiveTransactions { get; set; }
public Account Account { get; set; }
public ApplicationUser()
{
}
}
Transaction
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;
}
}
CodePudding user response:
public object ReceiverId { get; internal set; }
This property is causing the error, because it is not a type that entity framework recognizes for mapping. Either change the data type to a primitive like int or string, or remove the property.