CSharp · Console
// @nuget: EntityFramework
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()
{
InsertData();
using (var context = new BookStore())
{
var author = context.Authors
.Where(a => a.AuthorId == 2)
.FirstOrDefault();
context.Authors.Remove(author);
context.SaveChanges();
var authors = context.Authors.ToList();
FiddleHelper.WriteTable(authors);
}
}
public static void InsertData()
{
using (var context = new BookStore())
{
Author author1 = new Author()
{
FirstName = "Mark",
LastName = "Cuban",
};
Author author2 = new Author()
{
FirstName = "Andy",
LastName = "Russell",
};
Author author3 = new Author()
{
FirstName = "Johnny",
LastName = "Bravo",
};
context.Authors.Add(author1);
context.Authors.Add(author2);
context.Authors.Add(author3);
context.SaveChanges();
}
}
public class BookStore : DbContext
{
public BookStore() : base(FiddleHelper.GetConnectionStringSqlServer())
{
}
public DbSet<Author> Authors { get; set; }
}
public class Author
{
public int AuthorId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}