在所有类中使用通用属性

时间:2019-04-03 06:01:41

标签: c# entity-framework

我正在研究EF代码优先方法Web API。我几乎没有几个共同的属性,我想使其成为一个共同的类,并在各个模型中使用它。

现在,当我尝试使用PMC更新数据库时,出现以下错误,

  

值不能为空。

     

参数名称:entitySet

但是,如果我在模型中删除了通用类属性并进行了更新,它将很好地工作。

型号:

 public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        [Required, MaxLength(50)]
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime DOB { get; set; }
        public int City { get; set; }
        public int State { get; set; }
        public int Country { get; set; }
        public ComFields DateTimeDetails { get; set; } //This is the line
        [NotMapped]
        public List<Employee> Employees { get; set; }

    }

普通班:

 public class ComFields
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public DateTime CreatedDate { get; set; }
        public int? CreatedBy { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public DateTime ModifiedDate { get; set; }
        public int? ModifiedBy { get; set; }
        public bool Active { get; set; } = true;

    }

我在哪里错了?让我知道是否还有其他更好的方法来处理这种情况。

更新

试图覆盖种子方法以添加值,

protected override void Seed(ExerciseWebAPI.Context.DatabaseContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.

            context.Users.AddOrUpdate(x => x.UserId,
                new Users { Name = "aaa", Password = "CA", Email = "c@c.com", MobileNumber = "1234567", Age = 31, CountryId = 1, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, CreatedBy = 0, ModifiedBy = 0, Active = true },
                new Users { Name = "bbb", Password = "DA", Email = "a@a.com", MobileNumber = "1234987", Age = 31, CountryId = 2, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, CreatedBy = 0, ModifiedBy = 0, Active = true },
                new Users { Name = "ccc", Password = "BA", Email = "e@e.com", MobileNumber = "9876543", Age = 31, CountryId = 5, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, CreatedBy = 0, ModifiedBy = 0, Active = true }

                );
        }

0 个答案:

没有答案