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

标签 c# entity-framework entity-framework-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]
     }
   }

最佳答案

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

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

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

关于c# - 播种测试数据库时出错 - 无法将属性配置为导航属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27782465/

相关文章:

c# - 为什么我不能在 Visual Studio 2012 上使用 CodeFirst 创建数据库?

c# - ASP.NET Core 站点未在 IIS 上运行,从命令行运行正常

c# - ASP.NET 中的 GridView 问题

c# - 将 ASP.NET 站点直接连接到新数据库

c# - 通过 EF/Linq 转换到 KeyValuePair

c# - 如何一劳永逸地停止 Entity Framework 6 中的迁移?

c# - 我怎样才能得到 MediumDateString?

c# - F# 和 C# 中 COM 对象创建的差异

vb.net - 包管理器 : using Enable-Migrations for a specific project in the solution

entity-framework - Entity Framework 迁移级联删除始终为真,即使配置中的 WillCascadeOnDelete(false)