using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
namespace DatabaseExaple_6._0
{
internal class Program
{
static void Main(string[] args)
{
using (var db1 = new BookContext())
{
Book book1 = new Book() { Title = "book1", Author = "The author" };
db1.Books.Add(book1);
Book book2 = new Book() { Title = "book2", Author = "The author 2" };
db1.Books.Add(book2);
db1.SaveChanges();
var query = from b in db1.Books
orderby b.Title
select b;
Console.WriteLine("All books in the database:");
foreach (var b in query)
{
Console.WriteLine($"{b.Title} by {b.Author}, code={b.Code}");
}
Console.WriteLine("Press a key to exit...");
Console.ReadKey();
}
}
}
public class BookContext : DbContext {
public DbSet<Book> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(LocalDB)\MSSQLLocalDB;Database=Books;Integrated Security=True");
}
}
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
[Key] public int Code { get; set; }
}
}
报错提示:
System.InvalidOperationException:“An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' call.”