会议MVC教程中的错误1错误2

时间:2016-01-26 14:54:45

标签: c# asp.net-mvc

您好我跟随MVC tutorials given by microsoft系列,我遇到了这个问题:

  

错误1最佳重载方法匹配   'System.Data.Entity.DbSet.Add(Conference.Models.Session)'   视觉工作室有一些无效的论据   2013 \ Projects \ Conference \ Conference \ Models \ ConferenceContextInitializer.cs 18 31 Conference

     

错误2参数1:无法从'Conference.Models.Speaker'转换为   'Conference.Models.Session'视觉工作室   2013 \ Projects \ Conference \ Conference \ Models \ ConferenceContextInitializer.cs 19 34 Conference

ConferenceModelInitializer

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Conference.Models
{
    public class ConferenceContextInitializer : DropCreateDatabaseAlways<ConferenceContext>
    {
        protected override void Seed(ConferenceContext context)
        {
            context.Sessions.Add(
                new Session()
                {
                    Title = "I Want Spaghetti",
                    Abstract = "The Life and times of a spaghetti lover",
                    Speaker = context.Speakers.Add(
                                 new Speaker()
                                 {
                                     Name = "Jon Pepe",
                                     EmailAddress = "jon@asfdasd.com"
                                 })
                });
            context.SaveChanges();
        }
    }
}

ConferenceContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Conference.Models
{
    public class ConferenceContext : DbContext
    {
        public DbSet<Session> Sessions { get; set; }
        public DbSet<Session> Speakers { get; set; }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

取代:

public class ConferenceContext : DbContext
{
    public DbSet<Session> Sessions { get; set; }
    public DbSet<Session> Speakers { get; set; }
}

使用:

public class ConferenceContext : DbContext
{
    public DbSet<Session> Sessions { get; set; }
    public DbSet<Speaker> Speakers { get; set; } //since you are referencing the speaker class here
}