我正在尝试做一个简单的MVC项目。我们的想法是从数据库中选择信息并将其显示在地图上。这是以前未写入MVC的作业的升级/替换。需要新功能,MVC应该易于(维护)。
数据库是供应商数据库,我不做任何更改。我添加了4个视图:
基本计划是列出事件,附加的驾驶员和车辆信息,以及从事件开始到事件结束的一系列日志(+任何一个阈值)。
这是 MainContext 类
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
namespace MapsMVC.Models
{
public class MainContext : DbContext
{
public DbSet<VehicleModel> Vehicles { get; set; }
public DbSet<IncidentModel> Incidents { get; set; }
public DbSet<DriverModel> Drivers { get; set; }
public DbSet<LogsModel> Logs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<VehicleModel>().ToTable("vwMapsVehicles");
modelBuilder.Entity<IncidentModel>().ToTable("vwMapsIncidents");
modelBuilder.Entity<DriverModel>().ToTable("vwMapsDrivers");
modelBuilder.Entity<LogsModel>().ToTable("vwMapsLogs");
base.OnModelCreating(modelBuilder);
}
}
}
这是 IncidentModel 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using DataAnnotationsExtensions;
namespace CtrackMapsMVC.Models
{
public class IncidentModel
{
[Key]
[Integer]
[Min(0)]
[Display(Name = "Incident Id")]
public int IncidentId { get; set; }
[Integer]
[ForeignKey("Vehicles")]
[Display(Name = "Vehicle Id")]
public string NodeId { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm:ss}", ApplyFormatInEditMode = true)]
[Display(Name = "Incident Start")]
public DateTime IncidentStart { get; set; }
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm:ss}", ApplyFormatInEditMode = true)]
[Display(Name = "Incident End")]
public DateTime IncidentEnd { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Location")]
public string Location { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Incident Type")]
public string IncidentType { get; set; }
[Integer]
[Min(0)]
[ForeignKey("Logs")]
[Display(Name = "First Log Id")]
public int FirstLogId { get; set; }
[Integer]
[Min(0)]
[ForeignKey("Logs")]
[Display(Name = "Last Log Id")]
public int LastLogId { get; set; }
[Integer]
[Min(0)]
[ForeignKey("Drivers")]
[DisplayFormat(NullDisplayText = "No Driver")]
[Display(Name = "Driver Node Id")]
public int DriverNodeId { get; set; }
public virtual VehicleModel Vehicle { get; set; }
public virtual DriverModel Driver { get; set; }
public virtual ICollection<LogsModel> Logs { get; set; }
}
}
这是 VehicleModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
namespace CtrackMapsMVC.Models
{
public class VehicleModel
{
[Key]
[Integer]
[Min(0)]
[Editable(false)]
[Display(Name = "Node Id")]
public int NodeId { get; set; }
[Required]
[DataType(DataType.Text)]
[Editable(false)]
[Display(Name = "Vehicle Name")]
public string VehicleName { get; set; }
[DataType(DataType.Text)]
[Editable(false)]
[Display(Name = "Vehicle Description")]
public string VehicleDescription { get; set; }
[DataType(DataType.Text)]
[Editable(false)]
[Display(Name = "Cellnumber")]
public string Cellnumber { get; set; }
[DataType(DataType.DateTime)]
[Editable(false)]
[Display(Name = "Last Received")]
public DateTime LastReceived { get; set; }
[DataType(DataType.Text)]
[Editable(false)]
[Display(Name = "Unit Type")]
public string NodeTypeDesc { get; set; }
[DataType(DataType.Text)]
[Editable(false)]
[Display(Name = "Site Code")]
public string SiteCode { get; set; }
}
}
LogsModel 和 DriverModel 非常简单。
网站编译没有问题,控制器是默认生成的样板文件。还会生成视图,但会删除编辑/删除页面,索引中的链接引用也会被删除。
主页加载。当尝试加载/ Vehicle / Index时,我得到以下异常:
属性&#39; NodeId&#39;上的ForeignKeyAttribute on type&#39; CtrackMapsMVC.Models.IncidentModel&#39;无效。导航属性&#39; Vehicles&#39;在依赖类型&#39; CtrackMapsMVC.Models.IncidentModel&#39;中找不到。 Name值应该是有效的导航属性名称。
我的外键申报出了什么问题?我该如何解决?有些阅读暗示它不会起作用,因为SQL VIews实际上没有FK关系(不可能定义)。有延伸会有所帮助吗?对视图进行编码确实不常见吗?
答案 0 :(得分:0)
您在此处设置的属性:[ForeignKey("Vehicles")]
是您设置为“车辆”的导航属性,但您包含public virtual VehicleModel Vehicle { get; set; }
,这是“车辆”而不是“车辆”。
答案 1 :(得分:0)
这是基于EntityTypeConfiguration<>
的映射有用的地方。给你一些编译时间的帮助:
internal class IncidentMap : EntityTypeConfiguration<IncidentModel>
{
public IncidentMap()
{
HasKey(x => x.IncidentId);
ToTable("vwMapsIncidents");
HasRequired(m => m.Vehicle)
.WithMany()
.HasForeignKey(m => m.NodeID);
//or if you added a 'Incidents' property on 'Vehicle'
HasRequired(m => m.Vehicle)
.WithMany(m => m.Incidents)
.HasForeignKey(m => m.NodeID);
}
}
用法:
public class MainContext : DbContext
{
public DbSet<VehicleModel> Vehicles { get; set; }
public DbSet<IncidentModel> Incidents { get; set; }
public DbSet<DriverModel> Drivers { get; set; }
public DbSet<LogsModel> Logs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new IncidentMap());
modelBuilder.Configurations.Add(new VehicleMap());
modelBuilder.Configurations.Add(new DriverMap());
modelBuilder.Configurations.Add(new LogsMap());
base.OnModelCreating(modelBuilder);
}
}
然后从模型中删除所有[ForeignKey]
属性。
祝你好运......