Dbcontext创建我从未做过的列

时间:2017-10-08 21:48:20

标签: c# asp.net-mvc entity-framework model-view-controller

我正在尝试创建一个以前获得ID的视图,该ID正常工作(在调试器中检查,ID是正确的),以调用方法:

   public ActionResult DetaljiNarudzbe(int id)
        {
            DetaljiNarudzbeViewModel model = new DetaljiNarudzbeViewModel();
            model.Narudzba = ctx.Naruzbee.Where(x => x.Id == id).First();
            model.StatusNarudzbe = ctx.StatusiNarudzbi.Where(x => x.Id == model.Narudzba.StatusNarudzbeId).FirstOrDefault();
            model.Primaoc = ctx.Primaoci.Where(x => x.Id == model.Narudzba.PrimaocId).FirstOrDefault();
            model.Adresa = ctx.Adrese.Where(x => x.Id == model.Narudzba.AdresaId).FirstOrDefault();
            model.Grad = ctx.Gradovi.Where(x => x.Id == model.Adresa.GradId).FirstOrDefault();
            model.StavkeNarudzbe = ctx.StavkeNarudzbi.Where(x => x.Narudzbe_Id == id).ToList();
            model.Klijent = ctx.Klijenti.Where(x => x.Id == model.Narudzba.KlijentId).FirstOrDefault();
            model.Korisnik = ctx.Korisnici.Where(x => x.Id == model.Klijent.KorisnikId).FirstOrDefault();
            return View("DetaljiNarudzbe", model);
        }

然而,它仍然在这部分崩溃

 model.StavkeNarudzbe = ctx.StavkeNarudzbi.Where(x => x.Narudzbe_Id == id).ToList();

它抛出异常,因为出于某种原因,我认为上下文创建了另一个名为Narudzbe_Id1的列,该列不能为null。

https://imgur.com/a/UFxXB - 给定异常的图像

进一步证明这是dbcontext的一个问题:

https://imgur.com/a/KEOe3

额外的列没有出现在SQL服务器端的数据库中,我从中获取数据。

如果有帮助,我会在下面发布其他相关课程:

   public class StavkaNarudzbe : IEntity
    {
        public int Id { get; set; }
        public bool IsDeleted { get; set; }
        public string Naziv { get; set; }
        public int Tezina { get; set; }
        public double Cijena { get; set; }
        public int Narudzbe_Id { get; set; }
        public virtual Narudzbe Narudzbe { get; set; }
    }

public class MojKontekst : DbContext
    {
        public MojKontekst() : base("DostavaConnString")
        {

        }
       public DbSet<Adresa> Adrese { get; set; }
       public DbSet<Grad> Gradovi { get; set; }
       public DbSet<DetaljiVozila> DetaljiVozilaa { get; set; }
       public DbSet<Klijent> Klijenti { get; set; }
       public DbSet<Korisnik> Korisnici { get; set; }
       public DbSet<Kurir> Kuriri { get; set; }
       public DbSet<Kvar> Kvarovi { get; set; }
       public DbSet<Obavijest> Obavijesti { get; set; }
       public DbSet<Narudzbe> Naruzbee { get; set; }
       public DbSet<Posiljka> Posiljke { get; set; }
       public DbSet<Prelazi> Prelazii { get; set; }
       public DbSet<Primaoc> Primaoci { get; set; }
        public DbSet<Skladiste> Skladista { get; set; }
        public DbSet<StatusNarudzbe> StatusiNarudzbi { get; set; }
        public DbSet<StavkaNarudzbe> StavkeNarudzbi { get; set; }
        public DbSet<Vozilo> Vozila { get; set; }
        public DbSet<VrstaVozila> VrsteVozila { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)    
        {   
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
        }

  public class DetaljiNarudzbeViewModel
    {
        public Klijent Klijent;
        public Korisnik Korisnik;
        public Narudzbe Narudzba;
        public List<StavkaNarudzbe> StavkeNarudzbe;
        public StatusNarudzbe StatusNarudzbe;
        public Primaoc Primaoc;
        public Adresa Adresa;
        public Grad Grad;
    }

  public class Narudzbe : IEntity
    {
        public int Id { get; set; }
        public bool IsDeleted { get; set; }
        public string SifraNarudzbe { get; set; }
        public DateTime DatumNarudzbe { get; set; }
        public bool Osigurano { get; set; }
        public bool BrzaDostava { get; set; }
        public int BrojPaketa { get; set; }
        public int KlijentId { get; set; }
        public virtual Klijent Klijent { get; set; }
        public int AdresaId { get; set; }
        public virtual Adresa Adresa { get; set; }
        public Nullable<int> PosiljkaId { get; set; }
        public virtual Posiljka Posiljka { get; set; }
        public int StatusNarudzbeId { get; set; }
        public virtual StatusNarudzbe StatusNarudzbe{ get; set; }
        public int PrimaocId { get; set; }
        public virtual Primaoc Primaoc { get; set; }
        public Nullable<System.DateTime> VrijemeIsporuke { get; set; }
        public int CijenaNarudzbe { get; set; }
    }

异常文本:无效的列名Narudzbe_Id1

1 个答案:

答案 0 :(得分:0)

这是实体框架试图遵循它的关系列的标准命名约定。

有关详细信息,请参阅:https://msdn.microsoft.com/en-us/library/jj819164(v=vs.113).aspx

由于您的外键列使用非标准名称(即Narudzbe_Id应该是NarudzbeId),您需要让EF知道如何链接模型。重命名类的属性以遵循此命名约定,或使用数据注释明确告知EF关于您的关系。

例如,尝试添加ForeignKey属性(在System.Componentmodel.Dataannotations.Schema名称空间中找到),如下所示:

 public class StavkaNarudzbe : IEntity
    {
        public int Id { get; set; }
        public bool IsDeleted { get; set; }
        public string Naziv { get; set; }
        public int Tezina { get; set; }
        public double Cijena { get; set; }
        public int Narudzbe_Id { get; set; }
        [ForeignKey("Narudzbe_Id")]
        public virtual Narudzbe Narudzbe { get; set; }
    }