错误种子设定测试数据库 - 无法将属性配置为导航属性
本文关键字:属性 配置 导航 种子 测试 数据库 测试数据 错误 | 更新日期: 2023-09-27 18:36:11
我正在尝试创建一个稍微简单的库存跟踪器,但很难播种我的测试数据库。我当前正在尝试重置数据库并使用以下命令执行新的代码优先迁移:
- update-database -targetmigration:"0" -force -verbose
- *Delete Current Migrations
- add-migration InitialCreate
- update-database
当我运行update-database -targetmigration:"0" -force -verbose
时,Package Manager Console
返回:The property 'Model_Id' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type.
这是我以INV_Models.cs
Model
的名义使用Model
的简单问题,还是我忽略了什么?目前,应用程序已成功生成,但在新迁移时失败。
我的主要模型是INV_Assets
它有[ForeignKey]
值来Manufacturers, Type, Model, Location, Vendor
和Status
:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using GridMvc.DataAnnotations;
using System.Web.Mvc;
using InventoryTracker.Models;
namespace InventoryTracker.Models
{
[GridTable(PagingEnabled = true, PageSize = 30)]
public class INV_Assets
{
// Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model.
public int Id { get; set; }
public int Model_Id { get; set; }
[ForeignKey("Model_Id")]
public virtual int model_id { get; set; }
[Required]
public int Manufacturer_Id { get; set; }
[ForeignKey("Manfacturer_Id")]
public virtual int manufacturer_id { get; set; }
[Required]
public int Type_Id { get; set; }
[ForeignKey("Type_Id")]
public virtual int type_id { get; set; }
[Required]
public int Location_Id { get; set; }
[ForeignKey("Location_Id")]
public virtual int location_id { get; set; }
public int Vendor_Id { get; set; }
[ForeignKey("Vendor_Id")]
public virtual int vendor_id { get; set; }
[Required]
public int Status_Id { get; set; }
[ForeignKey("Status_Id")]
public virtual int status_id { get; set; }
public string ip_address { get; set; }
public string mac_address { get; set; }
public string note { get; set; }
public string owner { get; set; }
public decimal cost { get; set; }
public string po_number { get; set; }
public int invoice_number{ get; set; }
[Required]
public string serial_number { get; set; }
[Required]
public string asset_tag_number { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? acquired_date { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? disposed_date { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime modified_date { get; set; }
public string modified_by { get; set; }
// Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.)
//public bool available { get; set; }
}
}
INV_Models.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace InventoryTracker.Models
{
public class INV_Models
{
public int Id { get; set; }
public string model_description { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime created_date { get; set; }
[Required]
public string created_by { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime modified_date { get; set; }
public string modified_by { get; set; }
}
}
TestDataSeed.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InventoryTracker.Models;
using InventoryTracker.DAL;
using WebMatrix.WebData;
namespace InventoryTracker.Helper
{
public class TestDataSeed
{
InventoryTrackerContext context = new InventoryTrackerContext();
public void SeedDatabase()
{
List<INV_Assets> invAssets = getAssets();
List<INV_Locations> invLocs = getLocations();
List<INV_Manufacturers> invManufacturers = getManufacturers();
List<INV_Models> invModels = getModels();
List<INV_Statuses> invStatuses = getStatuses();
List<INV_Types> invTypes = getTypes();
List<INV_Vendors> iinvVendors = getVendors();
}
#region Seed Assets
private List<INV_Assets> getAssets()
{
List<INV_Assets> testAssets = new List<INV_Assets>
{
new INV_Assets
{
Id = 1,
ip_address = "10.10.135.38",
mac_address = "10.10.177.44",
note = "",
owner = "John Smith",
cost = 35,
po_number = "G348",
invoice_number = 1447,
serial_number = "JX14582Y",
asset_tag_number = "293548195023",
acquired_date = Convert.ToDateTime(10212014),
disposed_date = null,
created_by = "Admin",
created_date = DateTime.Now,
location_id = 1,
manufacturer_id = 1,
model_id = 1,
status_id = 2,
type_id = 3,
vendor_id = 3
}
};
return testAssets;
}
#endregion
[Seed Locations]
[Seed Manufacturers]
#region Seed Models
private List<INV_Models> getModels()
{
List<INV_Models> testModels = new List<INV_Models>
{
new INV_Models
{
Id = 1,
model_description = "XTERAV12",
created_by = "Admin",
created_date = DateTime.Now
},
new INV_Models
{
Id = 2,
model_description = "5330",
created_by = "Admin",
created_date = DateTime.Now
},
new INV_Models
{
Id = 1,
model_description = "Sunblade 6000",
created_by = "Admin",
created_date = DateTime.Now
}
};
return testModels;
}
#endregion
[Seed Statuses]
[Seed Types]
[Seed Vendors]
}
}
当你有一对这样的属性时...
public int Model_Id { get; set; }
[ForeignKey("Model_Id")]
public virtual int model_id { get; set; }
。在 [ForeignKey
属性中引用的属性应该是导航属性,即对实体类模型中另一个实体的引用。所以Model_Id
不能成为int
.
我不知道你为什么有这些int
属性对,但是像这样的一对......
[ForeignKey("Model")]
public int Model_id { get; set; }
public virtual Model Model { get; set; }
。会说得通的。或。。。
public int Model_id { get; set; }
[ForeignKey("Model_id")]
public virtual Model Model { get; set; }
当属性位于引用属性上时,它应指向基元外键属性。