Reputation: 5568
I have a many-to-many relationship in my code and I am trying to seed the database. Here is my seed method:
var loc = new List<Location> {
new Location { LocationName = "Paradise Lane" },
new Location { LocationName = "81st Street" }
};
loc.ForEach(l => context.Locations.Add(l));
var soft = new List<Software> {
new Software { Title = "Adobe Creative Suite", ... Locations = loc.Single(s => s.LocationName = "Paradise Lane")}
};
soft.ForEach(s => context.Software.Add(s));
Here is my locations class:
public class Location
{
public int Id { get; set; }
[Required]
[StringLength(20)]
public string LocationName { get; set; }
public virtual ICollection<Software> Software { get; set; }
}
Here is my software class:
public class Software
{
public int Id { get; set; }
[Required]
[StringLength(128)]
public string Title { get; set; }
[Required]
[StringLength(10)]
public string Version { get; set; }
[Required]
[StringLength(128)]
public string SerialNumber { get; set; }
[Required]
[StringLength(3)]
public string Platform { get; set; }
[StringLength(1000)]
public string Notes { get; set; }
[Required]
[StringLength(15)]
public string PurchaseDate { get; set; }
public bool Suite { get; set; }
public string SubscriptionEndDate { get; set; }
//[Required]
//[StringLength(3)]
public int SeatCount { get; set; }
public virtual ICollection<Location> Locations { get; set; }
public virtual ICollection<SoftwarePublisher> Publishers { get; set; }
public virtual ICollection<SoftwareType> Types { get; set; }
}
I am getting two errors. One, it tells me I cannot implicitly convert string to bool. I didn't even know I was trying to do t hat. And two, cannot convert lambda express to delegate because soem fo the return types in the block are not implicitly convertable to the delegate return type. Is that a reference to the iCollection?
Upvotes: 1
Views: 818
Reputation: 4999
You missed an equals sign. Double equals for comparing.
loc.Single(s => s.LocationName = "Paradise Lane")
vs
loc.Single(s => s.LocationName == "Paradise Lane")
Also, you can't do a .Single()
into Locations because it is an ICollection
. Single
returns 1 object, not a collection. You should use .Where instead. Or you can implicitly declare an array in there and use your .Single() code inside it.
Edit: Also apparently .Where() doesn't cast nicely to ICollection. Add a ToArray and you'll get an array which is acceptable.
Upvotes: 1