实际提供值时,如何解决“由于没有提供值而无法添加实体类型'X的种子实体”的问题?

时间:2019-06-15 10:37:51

标签: c# asp.net-mvc asp.net-core entity-framework-core

由于这个错误,我在测试我的应用程序时遇到了问题,特别是The seed entity for entity type 'Artikal' cannot be added because there was no value provided。 问题在于,肯定是通过字典paroviTip和类Tip本身提供了值(表示Artikal类中的FK)。 我想使用modelBuilder扩展(这是.NET Core 2.2的正确方法)将一些数据播种到数据库中,但是由于这个特殊错误,无法添加迁移。

我几乎用完了所有选项,因此我需要对代码进行外部查看。

填充数据库的ModelBuilderExtensions:

using BestDeal.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BestDeal.AdapteriPodataka
{
    public static class ModelBuilderExtensions
    {
        public static void PopuniBazu(this ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Artikal>().HasData
                    (
                  new Artikal
                  {
                      NazivArtikla = "HP 250 G6",
                      CijenaArtikla = 700,
                      IdArtikla = 1,
                      KratkiOpis = "Vrhunski laptop za prosječnog korisnika!",
                      DetaljniOpis = "Povežite se sa cijenjenim HP 250 notebook računarima. Završajite poslovne zadatke sa Intel® tehnologijom, osnovnim alatima za saradnju koji su učitani na HP 250. Izdržljiva šasija pomaže zaštiti laptopa od strogosti dana.",
                      URLMaleSlike1 = "https://static.toiimg.com/photo/60104728/HP-15-BS542TU-2EY84PA-Laptop-Core-i3-6th-Gen4-GB1-TBDOS.jpg",
                      URLSlike1 = "https://images-na.ssl-images-amazon.com/images/I/81iq991JMEL._SL1500_.jpg",
                      TipArtikla = ParoviTip["Laptopi"],
                      tipNaziv = "Laptopi"
                  }/*There is 30 of these, shortened for easier overview*/  
                    );

        }
              private static Dictionary<string, Tip> paroviTip = new Dictionary<string, Tip>();
        public static Dictionary<string, Tip> ParoviTip
        {
            get
            {
                if (paroviTip.Count==0)
                {
                    var listaTipova = new Tip[]
                    {
                    new Tip { Ime = "Laptopi", idTipa = 1 },
                    new Tip { Ime = "Mobiteli", idTipa = 2 },
                    new Tip { Ime = "Računari", idTipa = 3 },
                    new Tip { Ime = "Računarska oprema", idTipa = 4 }
                    };
                    foreach (Tip tipcic in listaTipova)
                    {
                        paroviTip.Add(tipcic.Ime, tipcic);
                    }
                }

                return paroviTip;
            }
        }
        }
    }

上下文文件:

using BestDeal.AdapteriPodataka;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BestDeal.Models
{
    //TODO: Skontati koji context ide, identitymodel, No authorization problem
    // You can add profile data for the user by adding more properties to your IdentityUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.

    //IdentityDbContext<IdentityUser> ???
    public class BestDealContext : IdentityDbContext<IdentityUser>
    {

        //automatske migracije
        private static readonly bool[] _migrated = { false };

        public BestDealContext(DbContextOptions<BestDealContext> options) : base(options)
        {
            /*  if (!_migrated[0])
                  lock (_migrated)
                      if (!_migrated[0])
                      {
                          Database.Migrate(); // apply all migrations
                          _migrated[0] = true;
                      }*/
           // Database.ExecuteSqlCommand("SET IDENTITY_INSERT Artikal ON");
        }
        public DbSet<IdentityUser> IdentityUser { get; set; }

        public DbSet<KorpaInfo> KorpaInfo { get; set; }
        public DbSet<Narudzba> Narudzba { get; set; }
        public DbSet<Obavijest> Obavijest { get; set; }
        public DbSet<ChatObavijest> ChatObavijest { get; set; }
        public DbSet<NarudzbeObavijest> NarudzbeObavijest { get; set; }
        public DbSet<Recenzija> Recenzija { get; set; }
       // public DbSet<StanjeNarudzbe> StanjeNarudzbe { get; set; }
        public DbSet<Tip> Tip { get; set; }
      //  public DbSet<Tipovi> Tipovi { get; set; }
        public DbSet<Artikal> Artikal { get; set; }
       public DbSet<Korpa> Korpa { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>().ToTable("IdentityUsers");
            modelBuilder.Entity<Korpa>().ToTable("Korpa");
            modelBuilder.Entity<KorpaInfo>().ToTable("KorpaInfo");
            modelBuilder.Entity<Narudzba>().ToTable("Narudzba");
            modelBuilder.Entity<Obavijest>().ToTable("Obavijest");
            modelBuilder.Entity<ChatObavijest>().ToTable("ChatObavijest");
            modelBuilder.Entity<NarudzbeObavijest>().ToTable("NarudzbeObavijest");
            modelBuilder.Entity<Recenzija>().ToTable("Recenzija");
            modelBuilder.Ignore<StanjeNarudzbe>();
            modelBuilder.Entity<Tip>().ToTable("Tip");
            modelBuilder.Ignore<Tipovi>();
            //Database.ExecuteSqlCommand("SET IDENTITY_INSERT Artikal ON");
            modelBuilder.Entity<Artikal>().ToTable("Artikal");
            ModelBuilderExtensions.PopuniBazu(modelBuilder);
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
        }


    }
}

Artikal.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using System.ComponentModel.DataAnnotations;
using static System.Net.Mime.MediaTypeNames;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.IdentityModel.Protocols;
using System.Data.SqlClient;
using System.Text;
using System.Diagnostics;
using BestDeal.AdapteriPodataka;

namespace BestDeal.Models
{
    public class Artikal
    {
        private string nazivArtikla;
        private string kratkiOpis;
        private string detaljniOpis;
        private string URLSlike;
        private string URLMaleSlike;
        private Tip tipArtikla;
        double cijenaArtikla;
        //TODO: Mozda lista recenzija umjesto specificne ocjene artikla, ljepse izgleda
        double ocjenaArtikla;
        static int id = 100;
        int idArtikla = 110;
        [ScaffoldColumn(false)]
        private List<Recenzija> recenzije=new List<Recenzija>();

        public Artikal()
        {
            id++;
            Debug.WriteLine("dodijelio id {0}", Program.lastArtikalId);
            idArtikla = Program.lastArtikalId;
            Program.lastArtikalId++;
        }

        public Artikal(double cijenaArtikla, int idArtikla)
        {
            CijenaArtikla = cijenaArtikla;
            this.idArtikla = idArtikla;
           idArtikla = Program.lastArtikalId;
            Program.lastArtikalId++;
        }
        String tippp;
        [System.ComponentModel.DisplayName("Naziv tipa artikla")]
        public String tipNaziv {
            get
            {
                return tippp;
            }
            set
            {

                tippp = value;
              /*  Tip tip = new Tip();
                tip.Ime = value;
                Program.lastTipId++;
                tip.idTipa = Program.lastTipId;
                TipArtikla = tip;*/
            }
            }
        //TODO: Ovdje bi mozda bio koristan flyweight large-scale jer slike mogu biti velike u slucaju nekoliko hiljada artikala
        //public Image SlikaArtikla { get; }
        [Required]
        public Tip TipArtikla
        {
            get => tipArtikla;
            set
            {
                //ako postoji tip ne treba dodavati novi
                foreach (Tip t in Tipovi.getInstance().ListaTipova.ToList())
                    if (t.Equals(value)) tipArtikla = value;
                    else
                    {
                       /* Program.lastTipId++;
                        tipArtikla.idTipa = Program.lastTipId;*/
                        tipArtikla = value;
                        //ako je dodan tip koji ne postoji u listi tipova, automatski se azurira ta lista
                        Tipovi.getInstance().ListaTipova.Add(value);
                    }
            }
        }
        [Required(ErrorMessage ="This field is required.")]
        [System.ComponentModel.DisplayName("Naziv artikla")]
        public string NazivArtikla { get => nazivArtikla; set => nazivArtikla = value; }

        [Required(ErrorMessage = "This field is required.")]
        [System.ComponentModel.DisplayName("Cijena artikla")]
        public double CijenaArtikla { get => cijenaArtikla; set => cijenaArtikla = value; }
        [ScaffoldColumn(false)]
        [System.ComponentModel.DisplayName("Ocjena artikla")]
        public double OcjenaArtikla { get => ocjenaArtikla; }// set => ocjenaArtikla = value; }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int IdArtikla { get => idArtikla; set => idArtikla = value; }
        [Required(ErrorMessage = "This field is required.")]
        [System.ComponentModel.DisplayName("Kratki opis")]
        public string KratkiOpis { get => kratkiOpis; set => kratkiOpis = value; }
        [Required(ErrorMessage = "This field is required.")]
        [System.ComponentModel.DisplayName("Detaljni opis")]
        public string DetaljniOpis { get => detaljniOpis; set => detaljniOpis = value; }
        [Required(ErrorMessage = "This field is required.")]
        [System.ComponentModel.DisplayName("URL slike")]
        public string URLSlike1 { get => URLSlike; set => URLSlike = value; }
        [Required(ErrorMessage = "This field is required.")]
        [System.ComponentModel.DisplayName("URL male slike")]
        public string URLMaleSlike1 { get => URLMaleSlike; set => URLMaleSlike = value; }

        public void DodajRecenziju(string tekstRecenzije, double ocjenaArtikla)
        {
            recenzije.Add(new Recenzija(tekstRecenzije, OcjenaArtikla));

            IzracunajOcjenu();
        }

        void IzracunajOcjenu()
        {
            double suma = 0.0;

            foreach (Recenzija recenzija in recenzije)
            {
                suma += recenzija.OverallRating;
            }

            ocjenaArtikla = suma / recenzije.Count;
        }



    }
}

Tip.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace BestDeal.Models
{
    //klasa koja omogucava dodavanje tipova
    public class Tip
    {
        public Tip()
        {
        }
        //TODO:Moguce opcije nekog IDa ili liste specificnih polja koju ima svaki tip (radi razlicitih detalja kod recenzija i sl.)
        public Tip(string ime)
        {
            Ime = ime;
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int idTipa { get; set; }
        public string Ime { get;  set; }
        public override bool Equals(object obj)
        {
            var other = obj as Tip;
            if (other == null)
            {
                return false;
            }
            return other == this;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(idTipa);
        }

        public static bool operator ==(Tip Tip1, Tip Tip2)
        {
            if (Object.ReferenceEquals(Tip1, null) && Object.ReferenceEquals(Tip2, null))
                return true;

            if (Object.ReferenceEquals(Tip1, null) || Object.ReferenceEquals(Tip2, null))
                return false;

            return Tip1.Ime == Tip2.Ime;
        }
        public static bool operator !=(Tip Tip1, Tip Tip2)
        {
            return !(Tip1 == Tip2);
        }
    }
}


我希望可以进行种子操作,并且能够将数据库条目成功迁移回Azure。如果存在这样的可能性,我会敞开心for。

1 个答案:

答案 0 :(得分:2)

您正在尝试使用具有以下特定要求的EF Core Model seed data

  

要添加具有关系的实体,需要指定外键

  

如果实体类型在阴影状态下具有任何属性,则可以使用匿名类提供值

很快,您不能导航属性用于这种类型的数据播种。

您的情况是什么意思

TipArtikla = ParoviTip["Laptopi"],

没有效果。

,您需要:

  1. 使用Tip的{​​{1}}和Artikal实体
  2. 对于HasData,可以在模型中添加显式的FK属性,或者使用匿名类型(由于缺乏编译时间支持,这可能很烦人并且容易出错)。在这两种情况下,上一行都应该变成

    Arktical