如何通过模型和初始化程序关联表

时间:2014-09-24 15:30:57

标签: entity-framework ef-code-first

我希望能够更好地理解如何将模型相互联系起来。我也在使用实体框架的种子数据。但是我觉得它设置不正确,我不知道在哪里。当我尝试运行公司的索引时,我得到一个不太有用的错误,专注于CompanysToList。我做了一件明显错误的事吗?我此刻迷路了

我的数据库设计很简单,从顶部开始,向下 - 管理员,公司,广告系列,想法。

以下四个类在我的模型文件夹中保存为单独的类。

public class Admin
{

    public int AdminID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }


    public virtual ICollection<Company> Companys { get; set; }
    public virtual ICollection<Campaign> Campaigns { get; set; }
    public virtual ICollection<Idea> Ideas { get; set; }
}

public class Company
{

    public int CompanyID { get; set; }
    public int AdminID { get; set; }
    public string CompanyName { get; set; }
    public string Website { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Campaign> Campaigns { get; set; }
    public virtual ICollection<Idea> Ideas { get; set; }
}

public class Campaign
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CampaignID { get; set; }
    public int CompanyID { get; set; }
    public int AdminID { get; set; }
    public string Description { get; set; }

    public virtual Idea Idea { get; set; }
    public virtual Company Company { get; set; }
}

排名在想法模型中。

public enum Rank
{
    A, B, C
}

public class Idea
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int IdeaID { get; set; }
    public int CampaignID { get; set; }
    public int CompanyID { get; set; }
    public int AdminID { get; set; }
    public string Description { get; set; }
    public Rank? Rank { get; set;}

}

然后我的种子数据

    protected override void Seed(CeptContext context)
    {

        var admins = new List<Admin>
        {
        new Admin{AdminID=1, FirstName="Bob",LastName="Test"},
        new Admin{AdminID=2, FirstName="Fred",LastName="Tester"},

        };

        admins.ForEach(s => context.Admins.Add(s));
        context.SaveChanges();

        var companys = new List<Company>
        {
        new Company{AdminID=1, CompanyID=101, CompanyName="Test Company",Website="www.testcompany.com",Description="Test Company"},
        new Company{AdminID=1, CompanyID=102, CompanyName="Big Company",Website="www.bigcompany.com",Description="big company"},
        new Company{AdminID=2, CompanyID=103, CompanyName="Big Technologies",Website="www.bigtechnologies.com",Description="Big technologies"},

        };

        companys.ForEach(s => context.Companys.Add(s));
        context.SaveChanges();

        var campaigns = new List<Campaign>
        {
        new Campaign{AdminID=1, CompanyID=101, CampaignID=201, Description="New campaign"},
        new Campaign{AdminID=1, CompanyID=101, CampaignID=202, Description="New campaign2"},
        new Campaign{AdminID=1, CompanyID=102, CampaignID=203, Description="New campaign3"},
        new Campaign{AdminID=2, CompanyID=103, CampaignID=204, Description="New campaign4"},

        };

        campaigns.ForEach(s => context.Campaigns.Add(s));
        context.SaveChanges();

        var ideas = new List<Idea>
        {
        new Idea{AdminID=1,CompanyID=101, CampaignID=201, IdeaID=301, Description="Big Idea", Rank=Rank.A},
        new Idea{AdminID=1,CompanyID=101, CampaignID=202, IdeaID=302, Description="Tiny Idea", Rank=Rank.B},
        new Idea{AdminID=1,CompanyID=102, CampaignID=203, IdeaID=303, Description="Idea to do great things", Rank=Rank.C},
        new Idea{AdminID=1,CompanyID=102, CampaignID=203, IdeaID=304, Description="Even bigger things", Rank=Rank.B},
        new Idea{AdminID=2,CompanyID=103, CampaignID=204, IdeaID=305, Description="Bad idea", Rank=Rank.C},
        new Idea{AdminID=2,CompanyID=103, CampaignID=204, IdeaID=306, Description="Other Idea", Rank=Rank.A},

        };
        ideas.ForEach(s => context.Ideas.Add(s));
        context.SaveChanges();


    }

0 个答案:

没有答案