【问题标题】:Creating a one-to-many relationship EF Core - update-database fail创建一对多关系 EF Core - 更新数据库失败
【发布时间】:2021-02-04 08:34:18
【问题描述】:

我想创建一对多的关系。

计划如下:一位客户可以拥有多辆自行车。下面我介绍数据模型。

自行车模型

public class Bike
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        [MaxLength(50)]
        public string Brand { get; set; }

        [Required]
        public int Size { get; set; }

        [Required]
        [MaxLength(200)]
        public string Description { get; set; }

        [Required]
        [MaxLength(50)]
        public string Model { get; set; }

        [ForeignKey("CustomerID")]
        public Guid CustomerID { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        public DateTime AddedBike { get; set; }
    }

客户模型

public class Customer
    {
        [Key]
        public Guid Id { get; set; }

        [StringLength(50, MinimumLength = 3)]
        [Required]
        public string Name { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 3)]
        public string Surname { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Required]
        [DisplayName("Telephone")]
        public string TelephoneNumber { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        public DateTime DateTimeAdd { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Last Update Customer")]
        public DateTime EditDate { get; set; }

        public virtual ICollection<Bike> Bike { get; set; }
    }

DBContext

public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {
        }

        public DbSet<Bike> Bikes { get; set; }
        public DbSet<Customer> Customers { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }

迁移进展顺利。但是,update-database 会导致此错误:

PM> 更新数据库 构建开始... 构建成功。 System.InvalidOperationException:没有实体类型映射到数据操作中使用的表“自行车”。要么将对应的实体类型添加到模型中,要么在数据操作中指定列类型。 ... 没有实体类型映射到数据操作中使用的表“自行车”。要么在模型中添加对应的实体类型,要么在数据操作中指定列类型。

我的错误在哪里?

【问题讨论】:

  • 如何更新数据库,可以分享一下初始模型吗?根据您的问题,我无法产生此错误。
  • 我不明白这个问题
  • 有了你的模型和DbContext,我可以成功迁移和更新数据库。你能分享一下你项目中的一些其他配置吗?
  • @KamilM 模型与 GitHub 存储库中的模型不匹配。
  • 是的,因为这个 repo 有最后的工作代码。当我更改模型时,更新数据库不起作用。

标签: c# entity-framework entity-framework-core


【解决方案1】:

终于成功了。我终于删除了所有的迁移,创建了一个新的迁移并更新了数据库。

然后我在模型中添加了关系,再次进行了迁移并更新了数据库并且它工作了。

我的模型是这样的

Bike.cs

public class Bike
    {
        [Key]
        public Guid Id { get; set; }

        [Required]
        [MaxLength(50)]
        public string Brand { get; set; }

        [Required]
        public int Size { get; set; }

        [Required]
        [MaxLength(200)]
        public string Description { get; set; }

        [Required]
        [MaxLength(50)]
        public string Model { get; set; }

        public Guid CustomerID { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        public DateTime AddedBike { get; set; }

        [ForeignKey("CustomerID")]
        public Customer Customer { get; set; }
    }

Customer.cs

 public class Customer
    {
        [Key]
        public Guid Id { get; set; }

        [StringLength(50, MinimumLength = 3)]
        [Required]
        public string Name { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 3)]
        public string Surname { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Required]
        [DisplayName("Telephone")]
        public string TelephoneNumber { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        public DateTime DateTimeAdd { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Last Update Customer")]
        public DateTime Edit { get; set; }

        public virtual ICollection<Bike> Bike { get; set; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多