EF - Many-to-Many Relationships (Data Annotations)

CSharp · Console

/Program.cs

// @nuget: EntityFramework
// @nuget: EntityFramework.SqlServerCompact
// @nuget: Microsoft.SqlServer.Compact
// @nuget: Z.EntityFramework.Extensions

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

public class Program
{
	public static void Main()
	{
		// Custom configuration specific for .NET Fiddle
		Z.EntityFramework.Extensions.EntityFrameworkManager.UseFiddleSqlCompact(System.Data.Entity.SqlServerCompact.SqlCeProviderServices.Instance, System.Data.SqlServerCe.SqlCeProviderFactory.Instance);
		
		using (var context = new BookStore())
		{	
			BookCategory bc1 = new BookCategory()
			{
				BookId = 1,
				CategoryId = 1
			};
			
			BookCategory bc2 = new BookCategory()
			{
				BookId = 1,
				CategoryId = 2
			};
			
			BookCategory bc3 = new BookCategory()
			{
				BookId = 2,
				CategoryId = 1
			};
			
			BookCategory bc4 = new BookCategory()
			{
				BookId = 2,
				CategoryId = 2
			};
			
			Book book1 = new Book()
			{
				Title = "MyBook 1",
				BookCategories = new List<BookCategory>()
				{
					bc1,
					bc2
				}
			};
			Book book2 = new Book()
			{
				Title = "MyBook 2",
				BookCategories = new List<BookCategory>()
				{
					bc3,
					bc4
				}
			};
			
			Category category1 = new Category()
			{
				CategoryName = "category 1",
				BookCategories = new List<BookCategory>()
				{
					bc1,
					bc3
				}
			};
			Category category2= new Category()
			{
				CategoryName = "category 2",
				BookCategories = new List<BookCategory>()
				{
					bc2,
					bc4
				}
			};
			
		    context.Books.Add(book1);
			context.Books.Add(book2);
			context.Categories.Add(category1);
			context.Categories.Add(category2);

		    context.SaveChanges();
			
			var categories = context.Categories.ToList();
			var books = context.Books.ToList();
			var bookCategories = context.BookCategories.ToList();
			FiddleHelper.WriteTable(books);
			FiddleHelper.WriteTable(categories);
			FiddleHelper.WriteTable(bookCategories);
		}
	}
	
	public class BookStore : DbContext
    {
        public BookStore() : base(@"Data Source=ZZZ_Projects.sdf")
        {
            
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }

        public DbSet<Category> Categories { get; set; }
        public DbSet<Book> Books { get; set; }
		public DbSet<BookCategory> BookCategories { get; set; }
    }

    public class Book
	{
    	public int BookId { get; set; }
    	public string Title { get; set; }
    	public virtual ICollection<BookCategory> BookCategories { get; set; }
	}

	public class Category
	{
	    public int CategoryId { get; set; }
	    public string CategoryName { get; set; }
	    public virtual ICollection<BookCategory> BookCategories { get; set; }
	}

	public class BookCategory
	{
    	[Key, Column(Order = 1)]
    	public int BookId { get; set; }
    	[Key, Column(Order = 2)]
    	public int CategoryId { get; set; }
    	public Book Book { get; set; }
    	public Category Category { get; set; }
	}
}
;