我对MVC很新,我在阅读两个模型之间的相关数据时遇到了问题,这两个模型没有导航属性。 我查看了以下主题,但没有任何运气: MVC4 how to load related data without Navigation Properties
这是我项目的一些图片的链接: http://s1067.photobucket.com/user/TimRH/slideshow/
我错过了什么或做错了什么?我的结果是空回来了
我希望有人可以帮助我朝着正确的方向前进
答案 0 :(得分:0)
您需要在表之间创建一对多的关系
主表
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int MAINTABLENAMEID { get; set; }
public virtual ICollection<CHILDTABLE> ALIAS { get; set; }
子表(添加导航属性)
[ForeignKey("ALIAS")]
public int [MAINTABLENAMEID] { get; set; }
public virtual [MAINTABLE] ALIAS { get; set; }
如果您无权更改数据库
您可以在SMSS中测试查询是否有效
using (var db = new ApplicationDbContext())
{
string Query = @"SELECT C.KritWert ,C.ArtNr,C.DLNr,C.SA
,C.Reserviert,C.GenArtNr,C.LKZ,C.LfdNr,C.SortNr,C.KritNr
,C.Exclude,C.AnzSofort,C.pk,k.KritWert,k.Amount,k.Brand
,k.Model,k.Type_Description
FROM C400_Article_Search_Tree_Allocation C
JOIN Ktype K
ON Convert(int,C.KritWert) = k.KritWert");
ObjectQuery<[NEWMODEL]> query = new ObjectQuery<[NEWMODEL]>
(Query,db, MergeOption.NoTracking);
}
答案 1 :(得分:0)
主要型号:
namespace CrossTest_V2.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class C400_Article_Search_Tree_Allocation
{
public string ArtNr { get; set; }
public Nullable<int> DLNr { get; set; }
public Nullable<int> SA { get; set; }
public string Reserviert { get; set; }
public Nullable<int> GenArtNr { get; set; }
public string LKZ { get; set; }
public Nullable<int> LfdNr { get; set; }
public Nullable<int> SortNr { get; set; }
public Nullable<int> KritNr { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string KritWert { get; set; }
public Nullable<int> Exclude { get; set; }
public Nullable<int> AnzSofort { get; set; }
public int pk { get; set; }
public virtual ICollection<Ktype> Ktype { get; set; }
}
}
儿童模特:
namespace CrossTest_V2.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class Ktype
{
[ForeignKey("KritWert")]
public int KritWert { get; set; }
public Nullable<int> Amount { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public string Type_Description { get; set; }
public virtual C400_Article_Search_Tree_Allocation C400Ktype { get; set; }
}
}
视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using CrossTest_V2.Models;
namespace CrossTest_V2.ViewModels
{
public class CrossViewModel : DbContext
{
public List<Cross> Crosses { get; set; }
public List<C203_Reference_Numbers> Refnr { get; set; }
public List<C203_Reference_Numbers> Refnr2 { get; set; }
public List<C400_Article_Search_Tree_Allocation> C400 { get; set; }
public List<C400_Article_Search_Tree_Allocation> C400_2 { get; set; }
}
}
控制器:
TCDEntities db = new TCDEntities();
public ActionResult Index()
{
CrossViewModel vm = new CrossViewModel();
vm.Crosses = db.Crosses.Take(1).ToList();
Cross cross = db.Crosses.FirstOrDefault();
// List<C400_Article_Search_Tree_Allocation> C400 = db.C400_Article_Search_Tree_Allocation.Where(k => k.ArtNr == cross.Article_From && k.DLNr == cross.Supplier_From && k.KritNr == 2).ToList();
vm.Refnr = db.C203_Reference_Numbers.Where(r => r.ArtNr == cross.Article_From && r.DLNr == cross.Supplier_From).ToList();
vm.Refnr2 = db.C203_Reference_Numbers.Where(r => r.ArtNr == cross.Article_To && r.DLNr == cross.Supplier_To).ToList();
// C400_Article_Search_Tree_Allocation article = db.C400_Article_Search_Tree_Allocation.Where(k => k.ArtNr == cross.Article_From && k.DLNr == cross.Supplier_From && k.KritNr == 2);
vm.C400 = db.C400_Article_Search_Tree_Allocation.Where(k => k.ArtNr == cross.Article_From && k.DLNr == cross.Supplier_From && k.KritNr == 2).ToList();
vm.C400_2 = db.C400_Article_Search_Tree_Allocation.Where(k => k.ArtNr == cross.Article_To && k.DLNr == cross.Supplier_To && k.KritNr == 2).ToList();
//var C400Ktype = db.C400_Article_Search_Tree_Allocation.Where(k => k.ArtNr == cross.Article_From && k.DLNr == cross.Supplier_From && k.KritNr == 2).ToList();
return View(vm);
}