如何在Entity Framework Code First中创建外键关系

时间:2012-12-27 17:37:29

标签: asp.net-mvc ef-code-first foreign-key-relationship

我是Entity Framework的新手,我正在使用ASP.NET MVC4项目。我正在努力创建一个外键关系并且没有取得多大成功。问题可能出在我的种子数据中,我不确定如何重新构建早期的尝试来实现这一点。

以下是我的两个课程:

public class HardwareType
    {
        public int Id { get; set; }
        [Required]
        [StringLength(128)]
        public string HType { get; set; }
        public int HardwareId { get; set; }
        public virtual Hardware Hardware { get; set; }


    }

public class Hardware
    {
        public int Id { get; set; }
        //public int HardwareId { get; set; }
        ...
        public virtual ICollection<HardwareType> HardwareType { get; set; }

    }

以下是我的示例种子数据:

protected override void Seed(Context context)
    {
        var loc = new List<Location> {
            new Location { LocationName = "Paradise Lane" },
            new Location { LocationName = "81st Street" }
        };
        loc.ForEach(l => context.Locations.Add(l));

        var type = new List<SoftwareType> {
            new SoftwareType { SType = "Suite" }
        };
        type.ForEach(t => context.SoftwareTypes.Add(t));

        var pub = new List<SoftwarePublisher> {
            new SoftwarePublisher { Publisher = "Adobe" },
            new SoftwarePublisher { Publisher = "Apple" },
            new SoftwarePublisher { Publisher = "Microsoft" }
        };

        var soft = new List<Software> {
            new Software { Title = "Adobe Creative Suite", Version = "CS6", SerialNumber = "1234634543", Platform = "Mac", Notes = "Macs rock!", PurchaseDate = "2012-12-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "Paradise Lane").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Adobe").ToArray() },
            new Software { Title = "Microsoft Office", Version = "2012", SerialNumber = "34252345", Platform = "PC", Notes = "PCs stink!", PurchaseDate = "2012-11-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "81st Street").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Microsoft").ToArray() },
            new Software { Title = "Apple iLife", Version = "2012", SerialNumber = "54675747564", Platform = "Mac", Notes = "Macs still rock!", PurchaseDate = "2012-12-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "Paradise Lane").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Apple").ToArray() }
        };
        soft.ForEach(s => context.Software.Add(s));

        var manuf = new List<Manufacturer> {
            new Manufacturer { ManufacturerName = "SonicWall" },
            new Manufacturer { ManufacturerName = "Cisco" },
            new Manufacturer { ManufacturerName = "Barracuda" },
            new Manufacturer { ManufacturerName = "Dell" },
            new Manufacturer { ManufacturerName = "HP" },
            new Manufacturer { ManufacturerName = "Maxtor" },
            new Manufacturer { ManufacturerName = "LaCie" },
            new Manufacturer { ManufacturerName = "APC" },
            new Manufacturer { ManufacturerName = "Intel" },
            new Manufacturer { ManufacturerName = "D-Link" },
            new Manufacturer { ManufacturerName = "Western Digital" },
            new Manufacturer { ManufacturerName = "Quantum" },
            new Manufacturer { ManufacturerName = "Seagate" },
            new Manufacturer { ManufacturerName = "Apple" },
            new Manufacturer { ManufacturerName = "Canon" },
        };

        var device = new List<DeviceType> {
            new DeviceType { DType = "Network Device"},
            new DeviceType { DType = "Other"}
        };

        var htype = new List<HardwareType> {
            new HardwareType { HType = "PC" },
            new HardwareType { HType = "Monitor" },
            new HardwareType { HType = "Printer" },
            new HardwareType { HType = "Miscellaneous" }
        };

        var hard = new List<Hardware> {
            new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped",  HardwareType = htype.Where(h => h.HType == "Monitor").ToArray()},
            new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped",  HardwareType = htype.Where(h => h.HType == "PC").ToArray() },
            new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped",  HardwareType = htype.Where(h => h.HType == "PC").ToArray() }
        };
        hard.ForEach(h => context.Hardwares.Add(h));

        base.Seed(context);

    }

我如何构建它以便外键关系起作用。现在,我收到此错误:Unable to set field/property HardwareType on entity type CIT.Models.Hardware. See InnerException for details.

内部例外是:An item cannot be removed from a fixed size Array of type 'CIT.Models.HardwareType[]'

1 个答案:

答案 0 :(得分:1)

尝试改变你的表结构:

public class Hardware 
{ 
public int Id { get; set; } 
. 
. 
. 
public HardwareTypeId { get; set; } 
public virtual HardwareType hardwareType {get;set;} 

}

和HardwareType:

public class HardwareType 
{ 
public int Id { get; set; } 
public string TypeName {get;set;} 
}