无法为EntityFrameworkCore多对一关系创建数据种子对象

时间:2019-06-11 08:46:25

标签: c# asp.net-core-2.0 many-to-one entity-framework-core-2.2

我想创建可用作种子数据的对象。但是当我遇到多对一表关系时,我会得到堆栈。我也尝试过很多对很多的关系,但是每次我到达同一点时(多对一)。如果是一对多,那不是问题:

Database Tables relation

表创建代码:

public class CorporateCustomer
{   
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CorporateCutomerId { get; set; }

    [Required]
    [MinLength(5, ErrorMessage = "Customer name cann't be less then 5 charecter !")]
    public string Name { get; set; }

    public virtual ICollection<CorporateCustomerContact> Contacts{ get; set; }
    public virtual ICollection<CorporateCustomerAddress> Addresses { get; set; }
    public virtual ICollection<CorporateCustomerFleet> Fleets { get; set; }
}

 public class CorporateCustomerAddress
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AddressId { get; set; }
    [Required]
    [MaxLength(30, ErrorMessage = "House Number needs to be less then 30 charecter in length")]
    public string HouseNo { get; set; }
    [Required]
    [MaxLength(20, ErrorMessage = "Road name needs to be less then 20 charecter")]
    public string Road { get; set; }
    [Required]
    [MaxLength(20, ErrorMessage = "City name needs to be less then 20 charecter")]
    public string City { get; set; }
    [Required]
    [StringLength(10, MinimumLength = 3, ErrorMessage = "Post code needs to be between 3 to 10 charecter in length")]
    public string PostCode { get; set; }
    [Required]
    [MaxLength(20, ErrorMessage = "Country name cannot be more than 20 charecter in length")]
    public string Country { get; set; }


    public bool IsDeleted { get; set; }


    // Relation with corporate customer
    public int CorporateCutomerId { get; set; }
    public CorporateCustomer CorporateCutomer { get; set; }   
}

我已经尝试过使用ModelBuilder类的解决方案,如下所示:

public static void SeedData(this ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CorporateCustomer>().HasData(new CorporateCustomer
        {
            CorporateCutomerId = 1,
            Name = "Micro Tech",
            Addresses = new List<CorporateCustomerAddress>()
            {
                modelBuilder.Entity<CorporateCustomerAddress>().HasData (
                    new CorporateCustomerAddress
                    {
                        AddressId = 1,
                        HouseNo = "36/B",
                        Road = "Lalkha Road, Fatullah",
                        City = "Dhaka",
                        PostCode = "14221"
                    })

            }
        });
    }

尝试过DbContext并不会真正插入数据库中

public static void SeedData(this ApplicationDbContext context)
    {
        var CorporateCustomerFleet = new List<CorporateCustomerFleet>()
        {
            new CorporateCustomerFleet
            {
                CorporateCutomer = new CorporateCustomer()
                {
                    Name = "Tech Realm",
                    Contacts = new List<CorporateCustomerContact>()
                    {
                        new CorporateCustomerContact()
                        {
                            Contact = "0088015AV757501",
                        },
                        new CorporateCustomerContact()
                        {
                            Contact = "0088018AB030022",
                        }
                    },
                    Addresses = new List<CorporateCustomerAddress>()
                    {
                        new CorporateCustomerAddress()
                        {
                            HouseNo = "36/B",
                            Road = "Fatullah, Lalkha Road",
                            City = "Dhaka"
                        }
                    }
                }
            }
        };
   Context.CorporateCustomerFleet.AddRange(CorporateCustomerFleet );
   Context.SaveChanges();
    }

0 个答案:

没有答案