Question

I have those two tables in my DataBase -

Material:

public class Material
    {
        [Required]
        [MaxLength(10)]
        public string Code { get; set; }


        [MaxLength(40)]
        public string Color { get; set; }

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

        [MaxLength(255)]
        public string Picture { get; set; }

        public long MaterialTypeId { get; set; }
        public virtual MaterialType MaterialType { get; set; }

    }

and MaterialType :

public class MaterialType { [MaxLength(40)] public string MatType { get; set; }

public virtual ICollection<Material> Materials { get; set; }

}

Then I call a method that populates all my tables with dummy data but the problem is that the Foreign Key could not be null and I still don't have data generated obviously. I tried to change the order the methods for creating the dummy data are called but this seems to not work. I wonder is there any easy way to work around this problem or maybe something that i don't know for managing this problem?

Was it helpful?

Solution

So in my case which is - the dummy data is created when the DB is created to work around the problem with Foreign Keys which could not be null the easiest way was to explicitly set the type of the property to nullable. As is in my code long?. This allows me to populate the table with data using null FK and then set the values I need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top