1

コードに多対多の関係があり、データベースをシードしようとしています。これが私のシードメソッドです:

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

ここに私の場所のクラスがあります:

public class Location
    {
        public int Id { get; set; }
        [Required]
        [StringLength(20)]
        public string LocationName { get; set; }
        public virtual ICollection<Software> Software { get; set; }
    }

これが私のソフトウェアクラスです:

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

    }

2 つのエラーが発生しています。1 つ目は、文字列をブール値に暗黙的に変換できないことを示しています。私は自分が帽子をかぶろうとしていたことさえ知りませんでした。そして 2 つ目は、ラムダ エクスプレスをデリゲートに変換できません。これは、ブロック内の戻り値の型がデリゲートの戻り値の型に暗黙的に変換できないためです。それはiCollectionへの参照ですか?

4

1 に答える 1

1

等号がありません。比較するための double equals。

loc.Single(s => s.LocationName = "Paradise Lane")

loc.Single(s => s.LocationName == "Paradise Lane")

また、.Single()これはICollection. Singleコレクションではなく、1 つのオブジェクトを返します。代わりに .Where を使用する必要があります。または、そこで配列を暗黙的に宣言し、その中で .Single() コードを使用することもできます。

編集:また、どうやら .Where() は ICollection にうまくキャストされません。ToArray を追加すると、受け入れ可能な配列が得られます。

于 2012-12-07T22:08:04.673 に答える