实体框架种子问题

时间:2013-10-21 07:25:53

标签: c# sql entity-framework

我创建了两个简单的实体'Member'和'Message'。每个成员可以有多个消息(一对多关系) 在尝试创建表时,我在迁移控制台中将此错误视为

Sequence contains no matching element

我的会员班

namespace MemberMessage.Models
{
    public class Member
    {
        //[Key]
        public int MemberID { get; set; }
        [Required]
        [StringLength(50)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [StringLength(50)]
        [Display(Name = "Middle Name")]
        public string MiddleName { get; set; }
        [StringLength(50)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Required]
        [StringLength(20, ErrorMessage = "User Name not more than 20 characters")]
        public string UserName { get; set; }
        [DataType(DataType.PhoneNumber, ErrorMessage = "Phone number is not in the correct format")]
        public string MobileNumber { get; set; }
        [DataType(DataType.EmailAddress)]
        public string Emailid { get; set; }
        [DataType(DataType.DateTime)]
        [DisplayFormat(DataFormatString = "{0:mm-dd-yy}", ApplyFormatInEditMode = true)]
        public DateTime DateOfBirth { get; set; }
        public string Address { get; set; }
        public string MessageTitle { get; set; }
        public string FullName { get { return FirstName + " " + MiddleName + " " + LastName; } }


        public virtual ICollection<Message> Messages { get; set; }


    }
}

消息CLass

namespace MemberMessage.Models
{
    public class Message
    {
        [Key]
        public int MessageID { get; set; }
        public int MemberID { get; set; }
        [Display(Name = "Messag Title")]
        public string MessageTitle { get; set; }

        public string MessageContent { get; set; }
        [DataType(DataType.DateTime)]
        [DisplayFormat(DataFormatString = "{0:dd-mm-yy}")]
        public DateTime MessageTime { get; set; }


        public virtual Member Member { get; set; }
    }
}

和播种的配置

 internal sealed class Configuration : DbMigrationsConfiguration<MemberMessage.DAL.MemContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(MemberMessage.DAL.MemContext context)
        {
            var members = new List<Member>
            {
                new Member{FirstName ="neil", MiddleName ="s",LastName ="fire1",DateOfBirth = DateTime.Parse("12-05-78"),Emailid ="jj@gmail.com", Address ="nothing",UserName ="fire" ,MobileNumber = "9089898909"},
                new Member{FirstName ="aeil", MiddleName ="s",LastName ="fire2",DateOfBirth = DateTime.Parse("12-06-78"),Emailid ="j7j@gmail.com", Address ="nothing",UserName ="fires" ,MobileNumber = "9089868949"},
                new Member{FirstName ="ceil", MiddleName ="s",LastName ="fire3",DateOfBirth = DateTime.Parse("12-07-78"),Emailid ="j6j@gmail.com", Address ="nothing",UserName ="fired" ,MobileNumber = "9089895449"},
                new Member{FirstName ="feil", MiddleName ="s",LastName ="fire4",DateOfBirth = DateTime.Parse("12-08-78"),Emailid ="j8j@gmail.com", Address ="nhothing",UserName ="firex" ,MobileNumber = "9083898949"},
                new Member{FirstName ="reil", MiddleName ="s",LastName ="fire5",DateOfBirth = DateTime.Parse("12-09-78"),Emailid ="j9j@gmail.com", Address ="nothiyng",UserName ="firel" ,MobileNumber = "9082898949"},
                new Member{FirstName ="jeil", MiddleName ="s",LastName ="fire6",DateOfBirth = DateTime.Parse("11-06-78"),Emailid ="j3j@gmail.com", Address ="nothihng",UserName ="firey" ,MobileNumber = "9189898949"},
                new Member{FirstName ="jril", MiddleName ="s",LastName ="fire7",DateOfBirth = DateTime.Parse("10-06-78"),Emailid ="j2j@gmail.com", Address ="nothing",UserName ="firet" ,MobileNumber = "9089868949"},
                new Member{FirstName ="eeil", MiddleName ="s",LastName ="fire8",DateOfBirth = DateTime.Parse("09-06-78"),Emailid ="j1j@gmail.com", Address ="notyhing",UserName ="fireu" ,MobileNumber = "9089898999"},
                new Member{FirstName ="heil", MiddleName ="s",LastName ="fire9",DateOfBirth = DateTime.Parse("12-06-88"),Emailid ="j0j@gmail.com", Address ="nothhing",UserName ="firei" ,MobileNumber = "9089898649"},
                new Member{FirstName ="ngtl", MiddleName ="s",LastName ="fire0",DateOfBirth = DateTime.Parse("12-06-98"),Emailid ="juj@gmail.com", Address ="nothiyng",UserName ="firen" ,MobileNumber = "9089828949"},
            };
            members.ForEach(m =>context.Members.AddOrUpdate(p => p.Emailid, m));
            context.SaveChanges();

            var messages = new List<Message>
            {
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="fire").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checksing",MessageContent = "nothing in this",MemberID = members .Single(s=>s.LastName =="fires").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="fired").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="firex").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="firel").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="firey").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="firet").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="fireu").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="firei").MemberID},
                    new Message {MessageTime = DateTime.Parse("12-08-98"), MessageTitle = "checkring",MessageContent = "nothing in this",MemberID = members.Single(s=> s.LastName =="firen").MemberID},

            };
            foreach (Message m in messages)
            {

                var messageindatabase = context.Messages.Where(s => s.MemberID == m.MemberID).SingleOrDefault();
                if (messageindatabase == null)
                {
                    context.Messages.Add(m);
                }
                context.SaveChanges();
            }

        }

1 个答案:

答案 0 :(得分:1)

members.Single(s=> s.LastName == ...)调用引发异常。这意味着没有单个Member符合指定的标准。

根据您输入Member的数据判断,您可能需要s => s.UserName而不是s => s.LastName