【问题标题】: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无法映射属性“属性名称”,因为它属于“对象”类型,不是受支持的原始类型或有效的实体类型
【发布时间】: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


【解决方案1】:
public object ReceiverId { get; internal set; }

此属性导致错误,因为它不是实体框架识别用于映射的类型。将数据类型更改为 int 或 string 等原始数据类型,或删除该属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 2013-07-05
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    相关资源
    最近更新 更多