获得EF 4.1 Code First Project Up&运行

时间:2011-09-23 23:41:44

标签: entity-framework-4.1 ef-code-first

我正在使用EF 4.1 Code First构建一个简单的应用程序,但我被卡住了。这是我的应用程序域模型的ERD:

enter image description here

在Visual Studio 2010中,我有 解决方案 ,其中包含 2个项目 。一个项目是存放域模型,另一个是用于存放应用程序逻辑的MVC3 Web应用程序。

在类库(项目1)中..我有以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace QuotesDomain
{
    public class QuoteContext : DbContext
    {
        public DbSet<Quote> Quotes { get; set; }
        public DbSet<Author> Authors { get; set; }
        public DbSet<Language> Languages { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<QTBridge> QTBridge { get; set; }
    }

    public class Quote
    {
        public int Id { get; set; }
        [Required, MaxLength(500)]
        public string Body { get; set; }
        public int Likes { get; set; }
        [Required]
        public bool isApproved { get; set; }
        [Required]
        public DateTime CreatedOn { get; set; }
        [Required]
        public virtual Author Author { get; set; }
        [Required]
        public virtual Language Language { get; set; }
        public virtual ICollection<QTBridge> TagsBridge { get; set; }
    }

    public class Author
    {
        public int Id { get; set; }
        [Required, MaxLength(30), MinLength(2)]
        public string FirstName { get; set; }
        [Required, MaxLength(30), MinLength(2)]
        public string LastName { get; set; }
        [Required]
        public DateTime DOB { get; set; }
        public DateTime DOD { get; set; }
        [Required, MaxLength(60), MinLength(2)]
        public string Occupation { get; set; }
        [Required, MaxLength(170), MinLength(5)]
        public string WikiLink { get; set; }
        public byte[] Image { get; set; }
        [Required]
        public bool isApproved { get; set; }
        [Required]
        public DateTime CreatedOn { get; set; }
        public virtual ICollection<Quote> Quotes { get; set; }
    }

    public class Language
    {
        public int Id { get; set; }
        [Required, MaxLength(20), MinLength(2)]
        public string Name { get; set; }
        public byte[] Image { get; set; }
        [Required]
        public DateTime CreatedOn { get; set; }
        public virtual ICollection<Quote> Quotes { get; set; }
    }

    public class Tag
    {
        public int Id { get; set; }
        [Required, MaxLength(40), MinLength(2)]
        public string Name { get; set; }
        public byte[] Image { get; set; }
        [Required]
        public DateTime CreatedOn { get; set; }
        public virtual ICollection<QTBridge> QuotesBridge { get; set; }
    }

    public class QTBridge
    {
        public int Id { get; set; }
        public int QuoteId { get; set; }
        public int TagId { get; set; }
    }
}

我一直在看一些视频教程,上面看起来很好,但是当我尝试运行应用程序时,我收到以下错误:

enter image description here

如果:

,我将不胜感激

1)您可以根据ERD图快速查看我的Code First类,看看它们是否正确

2)帮我解决错误。

非常感谢任何帮助

谢谢。

1 个答案:

答案 0 :(得分:0)

以下是实体和配置代码的完整列表。

public class QuoteContext : DbContext
{
    public DbSet<Quote> Quotes { get; set; }
    public DbSet<Author> Authors { get; set; }
    public DbSet<Language> Languages { get; set; }
    public DbSet<Tag> Tags { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //This will create create a join table "QuoteTags"
        modelBuilder.Entity<Quote>().HasMany(q => q.Tags)
            .WithMany(t => t.Quotes);
    }
}

public class Quote
{
    public int Id { get; set; }

    [Required, MaxLength(500)]
    public string Body { get; set; }

    public int Likes { get; set; }

    [Required]
    public bool IsApproved { get; set; }

    [Required]
    public DateTime CreatedOn { get; set; }

    public int AuthorId { get; set; }

    [ForeignKey("AuthorId")]
    public virtual Author Author { get; set; }

    public int LanguageId { get; set; }

    [ForeignKey("LanguageId")]
    public virtual Language Language { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}

public class Author
{
    public int Id { get; set; }

    [Required, MaxLength(30), MinLength(2)]
    public string FirstName { get; set; }

    [Required, MaxLength(30), MinLength(2)]
    public string LastName { get; set; }

    [Required]
    public DateTime DOB { get; set; }

    public DateTime DOD { get; set; }

    [Required, MaxLength(60), MinLength(2)]
    public string Occupation { get; set; }

    [Required, MaxLength(170), MinLength(5)]
    public string WikiLink { get; set; }

    public byte[] Image { get; set; }

    [Required]
    public bool IsApproved { get; set; }

    [Required]
    public DateTime CreatedOn { get; set; }

    public virtual ICollection<Quote> Quotes { get; set; }
}

public class Language
{
    public int Id { get; set; }

    [Required, MaxLength(20), MinLength(2)]
    public string Name { get; set; }

    public byte[] Image { get; set; }

    [Required]
    public DateTime CreatedOn { get; set; }

    public virtual ICollection<Quote> Quotes { get; set; }
}

public class Tag
{
    public int Id { get; set; }

    [Required, MaxLength(40), MinLength(2)]
    public string Name { get; set; }

    public byte[] Image { get; set; }

    [Required]
    public DateTime CreatedOn { get; set; }

    public virtual ICollection<Quote> Quotes{ get; set; }
}