播种测试数据库时出错 - 无法将属性配置为导航属性?

Error Seeding Test Database - Property cannot be configured as navigation property?

我正在尝试创建一个比较简单的 InventoryTracker,但在为我的测试数据库设定种子时遇到了困难。我目前正在尝试使用以下命令重置数据库并执行新的代码优先迁移:

 - 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, VendorStatus:

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 属性中引用的 属性 应该是导航 属性,即对实体 class 模型中另一个实体的引用.所以 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; }

当属性在引用 属性 上时,它应该指向原始外键 属性。