C# 如何使用继承向.NET Core 2.0中的aspNetUser表添加字段

C# 如何使用继承向.NET Core 2.0中的aspNetUser表添加字段,c#,asp.net,asp.net-mvc,asp.net-core,migration,C#,Asp.net,Asp.net Mvc,Asp.net Core,Migration,我使用aspNetUser表来存储 系统,所以我需要知道哪个国家和城市属于这些,为此我添加了以下模型城市和国家 public class Country : IEntity { public int Id { get; set; } [Required] [Display(Name = "Country")] [MaxLength(50, ErrorMessage = "The field {0} only can con

我使用aspNetUser表来存储 系统,所以我需要知道哪个国家和城市属于这些,为此我添加了以下模型城市和国家

public class Country : IEntity
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "Country")]
        [MaxLength(50, ErrorMessage = "The field {0} only can contain {1} characters length.")]
        public string Name { get; set; }

        public ICollection<City> Cities { get; set; }

        [Display(Name = "# Cities")]
        public int NumberCities { get { return this.Cities == null ? 0 : this.Cities.Count; } }
    }

public class City : IEntity
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "Ciudad")]
        [MaxLength(50, ErrorMessage = "el campo {0} solo puede contener {1} caracteres de largo.")]
        public string Name { get; set; }
    }
问题在于我的类SeaderDB.cs Seed数据库,每当我删除它以执行迁移时,它就会填充数据库,这正是我的程序崩溃的地方!组装用户类并将其插入数据库时

我的seader.cs类验证是否存在用户管理员。。。。如果没有,它将创建它,但它是在组装用户对象时落下的

SeedDB.CS:

 public async Task SeedAsync()
        {
            await this.context.Database.EnsureCreatedAsync();

            await userHelper.CheckRoleAsync("Dios");
            await userHelper.CheckRoleAsync("Admin");
            await userHelper.CheckRoleAsync("User");

            var user = await userHelper.GetUserByEmailAsync("coambiado@ing.ucsc.cl");
            if (user == null)
            {
                try
                {
                    user = new User
                    {
                        Nombre = "Cristofher",
                        Apellido = "Ambiado",
                        Email = "coambiado@ing.ucsc.cl",
                        UserName = "coambiado@ing.ucsc.cl",
                        PhoneNumber = "+56958987975",
                        EmailConfirmed = true,
                        Address = "Calle Luna Calle Sol",
                        CityId = context.Countries.FirstOrDefault().Cities.FirstOrDefault().Id,
                        City = context.Countries.FirstOrDefault().Cities.FirstOrDefault()
                    };
                }
                catch (Exception error)
                {

                }       

                var result = await userHelper.AddUserAsync(user, "fytitosk8");              
                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("No se pudo crear el usuario"); 
                }
                await userHelper.AddUserToRoleAsync(user, "Dios");
            }


            var isInRole = await userHelper.IsUserInRoleAsync(user, "Dios");
            if (!isInRole)
            {
                await userHelper.AddUserToRoleAsync(user, "Dios");
            }


            //AGREGAR PRODUCTOS
            if (!this.context.Productos.Any())
            {
                this.AddProductos("COMBO 1 + JUGO CITRICO",  user);
                this.AddProductos("ENSALADA CESAR + JUGO CITRICO", user);
                this.AddProductos("MENU VEGAN 1 + POSTRE",  user);
                this.AddProductos("MENU VEGAN 2 + POSTRE", user);

                await this.context.SaveChangesAsync();
            }

            //AGREGAR PAISES
            if (!this.context.Countries.Any())
            {
                var cities = new List<City>();

                cities.Add(new City { Name = "Concepción" });
                cities.Add(new City { Name = "Santiago" });
                cities.Add(new City { Name = "Iquique" });
                cities.Add(new City { Name = "Valdivia" });

                this.context.Countries.Add(new Country
                {
                    Cities = cities,
                    Name = "Chile"
                });

                await this.context.SaveChangesAsync();
            }

        }

我可以使用lamdas表达式来询问CityId的值吗?我的代码怎么了?我做得好吗?seader正在创造国家和城市,但在Linq的指示下无法获得它们。。。有什么帮助吗?

要添加用户,最好像其他字段一样使用默认值。 而不是这些代码行

比如你可以用这个


城市是与用户相关的城市类型属性。。。。它不可能是字符串,值得一提的是它的解决方案是有效的,它让我编译.Oo。我没有注意。你是对的。然后,您可以首先构建一个城市对象,然后添加该对象。我要更正我的答案
 public async Task SeedAsync()
        {
            await this.context.Database.EnsureCreatedAsync();

            await userHelper.CheckRoleAsync("Dios");
            await userHelper.CheckRoleAsync("Admin");
            await userHelper.CheckRoleAsync("User");

            var user = await userHelper.GetUserByEmailAsync("coambiado@ing.ucsc.cl");
            if (user == null)
            {
                try
                {
                    user = new User
                    {
                        Nombre = "Cristofher",
                        Apellido = "Ambiado",
                        Email = "coambiado@ing.ucsc.cl",
                        UserName = "coambiado@ing.ucsc.cl",
                        PhoneNumber = "+56958987975",
                        EmailConfirmed = true,
                        Address = "Calle Luna Calle Sol",
                        CityId = context.Countries.FirstOrDefault().Cities.FirstOrDefault().Id,
                        City = context.Countries.FirstOrDefault().Cities.FirstOrDefault()
                    };
                }
                catch (Exception error)
                {

                }       

                var result = await userHelper.AddUserAsync(user, "fytitosk8");              
                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("No se pudo crear el usuario"); 
                }
                await userHelper.AddUserToRoleAsync(user, "Dios");
            }


            var isInRole = await userHelper.IsUserInRoleAsync(user, "Dios");
            if (!isInRole)
            {
                await userHelper.AddUserToRoleAsync(user, "Dios");
            }


            //AGREGAR PRODUCTOS
            if (!this.context.Productos.Any())
            {
                this.AddProductos("COMBO 1 + JUGO CITRICO",  user);
                this.AddProductos("ENSALADA CESAR + JUGO CITRICO", user);
                this.AddProductos("MENU VEGAN 1 + POSTRE",  user);
                this.AddProductos("MENU VEGAN 2 + POSTRE", user);

                await this.context.SaveChangesAsync();
            }

            //AGREGAR PAISES
            if (!this.context.Countries.Any())
            {
                var cities = new List<City>();

                cities.Add(new City { Name = "Concepción" });
                cities.Add(new City { Name = "Santiago" });
                cities.Add(new City { Name = "Iquique" });
                cities.Add(new City { Name = "Valdivia" });

                this.context.Countries.Add(new Country
                {
                    Cities = cities,
                    Name = "Chile"
                });

                await this.context.SaveChangesAsync();
            }

        }
CityId = context.Countries.FirstOrDefault().Cities.FirstOrDefault().Id,
                    City = context.Countries.FirstOrDefault().Cities.FirstOrDefault()
user = new User
                {
                    Nombre = "Cristofher",
                    Apellido = "Ambiado",
                    Email = "coambiado@ing.ucsc.cl",
                    UserName = "coambiado@ing.ucsc.cl",
                    PhoneNumber = "+56958987975",
                    EmailConfirmed = true,
                    Address = "Calle Luna Calle Sol",
                    CityId = 1,
                    City = new City { Id =1, Name = "Concepción" } 
                };