テーブルにレコードを挿入できません。「シーケンスには複数の要素が含まれています」というエラーが表示されます。追加したいエンティティは、Table Per Hierachy (TPH) 関係の別のテーブルから継承します。
public abstract class Reward
{
public int RewardId { get; set; }
[Display(Name = "Retailer")]
public int RetailerId { get; set; }
[Display(Name = "Reward Name")]
public string RewardName { get; set; }
[Display(Name = "Start Date")]
[DataType(DataType.DateTime)]
public DateTime StartDate { get; set; }
[Display(Name = "Start Date")]
[DataType(DataType.DateTime)]
public DateTime EndDate { get; set; }
public string Description { get; set; }
public Boolean Status { get; set; }
[Display(Name = "Reward Type")]
public Enums.RewardType RewardType { get; set; }
public ICollection<Customer> EnrolledCustomers { get; set; }
public virtual Retailer BusinessName { get; set; }
}
次に、このエンティティは上記のエンティティを次のように拡張します
public class CashReward : Reward
{
[Display(Name = "Default Cash Value")]
public double DefaultCashValue { get; set; }
[Display(Name = "X Value - Amount Spent")]
public decimal XAmountSpent { get; set; }
[Display(Name = "Y Value - Amount Earned")]
public decimal YAmountEarned { get; set; }
}
上記のことから、Entity Framework は次のように報酬テーブルを作成しました (他の 2 つのクラスが報酬クラスを拡張していることに注意してください。ただし、ここでは現金報酬クラス/エンティティのみに焦点を当てています)。
CREATE TABLE [dbo].[Rewards] (
[RewardId] INT IDENTITY (1, 1) NOT NULL,
[RetailerId] INT NOT NULL,
[RewardName] NVARCHAR (MAX) NULL,
[StartDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
[Description] NVARCHAR (MAX) NULL,
[Status] BIT NOT NULL,
[RewardType] INT NOT NULL,
[DefaultCashValue] FLOAT (53) NULL,
[XAmountSpent] DECIMAL (18, 2) NULL,
[YAmountEarned] DECIMAL (18, 2) NULL,
[DefaultValue] INT NULL,
[CurrentNumberOfTransaction] INT NULL,
[TargetNumberOfTransaction] INT NULL,
[GetYFree] INT NULL,
[DefaultPointsValue] BIGINT NULL,
[XAmountSpent1] DECIMAL (18, 2) NULL,
[YPointsEarned] BIGINT NULL,
[Discriminator] NVARCHAR (128) NOT NULL,
CONSTRAINT [PK_dbo.Rewards] PRIMARY KEY CLUSTERED ([RewardId] ASC),
CONSTRAINT [FK_dbo.Rewards_dbo.Retailers_RetailerId] FOREIGN KEY ([RetailerId]) REFERENCES [dbo].[Retailers] ([RetailerId]) ON DELETE CASCADE
次のようにEF Code First Seedメソッドを使用して、タイプCashRewardの2つのレコードを追加しようとしています:
var cashRewards = new List<Reward>
{
new CashReward
{
RetailerId = retailers.Single( i => i.BusinessName == "Prime Lending").RetailerId,
RewardName = "October Fest Bonanza",
StartDate = DateTime.Now,
EndDate = DateTime.Parse("12/23/2014"),
Description = "Best reward for the month of October",
Status = true,
RewardType = Enums.RewardType.CashReward,
DefaultCashValue = 2,
XAmountSpent = 20,
YAmountEarned = 1
},
new CashReward
{
RetailerId = retailers.Single( i => i.BusinessName == "Mike Liquor Store").RetailerId,
RewardName = "Loyal Customer Reward",
StartDate = DateTime.Now,
EndDate = DateTime.Parse("2/9/2014"),
Description = "Hope you enjoy the best reward for the month of October",
Status = true,
RewardType = Enums.RewardType.CashReward,
DefaultCashValue = 5,
XAmountSpent = 40,
YAmountEarned = 5
},
};
cashRewards.ForEach(r => context.Reward.AddOrUpdate(p => p.RewardName, r));
context.SaveChanges();
そして、エラーメッセージが表示されます
シード メソッドを実行します。
System.InvalidOperationException: Sequence contains more than one element
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](DbSet`1 set, IEnumerable`1 identifyingProperties, InternalSet`1 internalSet, TEntity[] entities)
at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](IDbSet`1 set, Expression`1 identifierExpression, TEntity[] entities)
at ValueCard.Web.Migrations.Configuration.<>c__DisplayClass22.<Seed>b__1a(Reward r) in \Migrations\Configuration.cs:line 113
at System.Collections.Generic.List`1.ForEach(Action`1 action)
私を正しい方向に向けるのを手伝ってください。御時間ありがとうございます