【问题标题】:Getting EF 4.1 Code First project working让 EF 4.1 Code First 项目工作
【发布时间】:2011-09-23 23:41:44
【问题描述】:

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

在 Visual Studio 2010 中,我有一个包含两个项目的解决方案。一个项目是容纳域模型,另一个是一个 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; }
    }
}

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

根据 ERD 图,我的 Code First 类是否正确?我需要做什么来解决这个错误?

【问题讨论】:

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


【解决方案1】:

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

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; }
}

【讨论】:

  • 感谢埃兰加的回复。我已经将 [ virtual ] 关键字添加到导航属性中。不过,我仍然对 ForeignKey 感到有些困惑。我想做的就是拥有一个 Quote.Id 和 Tag.Id 的复合键 .. 实现这一目标的最佳方法是什么。谢谢。
  • @Ciwan 基于属性的配置(如上所述)或 Fluent API 可用于配置您的模型。这只是一个选择使用哪个的问题。
  • 我继续添加了您在答案中提供的 QTBridge 代码,但它仍然无法正常工作,我收到以下错误,我不确定如何继续。 :( 1Composite Key Error
  • @Ciwan 在使用 ORM 时,您通常不应该使用复合键,如果您给 QTBridge 自己的主键,它会更轻松
  • 感谢 Daveo,我刚刚注意到关系线在我的 ERD 图中的多对多关系中的方式是错误的。我已经按照你的建议 Daveo 做了,但现在我得到了一个非常 !vague error!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 2011-10-16
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多