错误种子测试数据库 - 属性不能配置为导航属性?

时间:2015-01-05 15:18:49

标签: c# entity-framework ef-migrations

我正在尝试创建一个有点简单的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.

这是我在Model INV_Models.cs名称中使用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]
     }
   }

1 个答案:

答案 0 :(得分:5)

当你有一对像这样的属性......

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; }

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