Getting EF 4.1 Code First project working
I am building a simple application with EF 4.1 Code开发者_开发百科 First, but I am stuck. Here's an ERD of my application Domain Model:
In Visual Studio 2010, I have a solution with two projects in it. One project is to house the Domain Model, the other is an MVC3 Web Application to house the application logic.
In the Class Libraries (Project 1), I have the following code:
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; }
}
}
I've been watching some video tutorials, and the above looks good to me, but when I try and run the application, I get the following error:
Are my Code First classes correct based on the ERD Diagram? What do I need to do to solve the error?
Here's the full listing of code for Entities and configuration.
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; }
}
精彩评论