使用ServiceStack和C#将数据发布到SQL Server

我刚刚开始学习servicestack和c#,并且需要一些将数据发布到sql server的帮助.当我使用swagger测试时,收到200响应,但实际上没有任何内容插入数据库,而且我不确定我要去哪里.

型号

public class Book
{
    [PrimaryKey]
    [AutoIncrement]
    public int BookID { get; set; }
    public string Author { get; set; }
    public string Title { get; set; }
    public int NumberOfPages { get; set; }
    public int Isbn { get; set; }
}

管理员介面:

namespace ServiceStackServiceLog4NetTemplate.Interfaces.Managers
{
   public interface IPostBookManager
  {
    Book CreateBooks();
  }
}

仓库接口:

namespace ServiceStackServiceLog4NetTemplate.Interfaces.Repositories
{
   public interface IPostBookRepository
   {
     Book PostBooks();
   }
}

消息请求

namespace ServiceStackServiceLog4NetTemplate.ServiceModel.Messages
{
[Route("/PostBooks", Verbs = "POST")]
public class PostBooksRequest
{
    [AutoIncrement]
    public int BookID { get; set; }
    public string Author { get; set; }
    public string Title { get; set; }
    public int NumberOfPages { get; set; }
    public int Isbn { get; set; }
}
}

消息响应

namespace ServiceStackServiceLog4NetTemplate.ServiceModel.Messages
{
public class PostBooksResponse : IHasResponseStatus
{
    public Book Book { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
}
}

经理

    class PostBooksManager : IPostBookManager
    {
    private IPostBookRepository postRepo;

    public PostBooksManager(IPostBookRepository pRepo)
    {
        postRepo = pRepo;
    }

    public Book CreateBooks()
    {
        var bookCreations = postRepo.PostBooks();
        return bookCreations;
    }
}
}

资料库

namespace ServiceStackServiceLog4NetTemplate.Repositories
{
public class PostBookSqlRepo : IPostBookRepository
{
    private readonly string connection = ConfigurationManager.AppSettings["BooksDB"];

    public Book PostBooks()
    {
        var postBooks = CreateBooks();
        return postBooks;
    }

    private Book CreateBooks()
    {
        var newBooks = new Book();
        string query = "INSERT INTO dbo.BooksTable(BookID, Author, Title, NumberOfPages, ISBN)" +
                       "VALUES(@BookID, @Author, @Title, @NumberOfPages, @ISBN)";
        SqlConnection dbConnect = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand(query, dbConnect);


        using (dbConnect)
        {
            dbConnect.Open();


            var b = new Book()
            {
                BookID = newBooks.BookID,
                Author = newBooks.Author,
                Title = newBooks.Title,
                NumberOfPages = newBooks.NumberOfPages,
                Isbn = newBooks.Isbn
            };

            cmd.Parameters.AddWithValue("@BookID", b.BookID);
            cmd.Parameters.AddWithValue("@Author", b.Author);
            cmd.Parameters.AddWithValue("@Title", b.Title);
            cmd.Parameters.AddWithValue("@NumberOfPages", b.NumberOfPages);
            cmd.Parameters.AddWithValue("@ISBN", b.Isbn);


            dbConnect.Close();
        }

        return newBooks;

    }

}
}

服务定义

namespace ServiceStackServiceLog4NetTemplate.ServiceDefinition
{
[EnableCors(allowedMethods: "GET,POST,DELETE")]
class PostBooksServiceDefinition : Service
{
    private IPostBookManager postManager;

    public PostBooksServiceDefinition(IPostBookManager bookP)
    {
        postManager = bookP;
    }

    public object Post(PostBooksRequest request)
    {
        var postBook = request.ConvertTo<Book>();

        PostBooksResponse resp = new PostBooksResponse()
        {
            Book = postBook
        };

        return resp;
    }
}
}

解决方法:

首先,您不应该在请求DTO上使用[AutoIncrement],而是将DTO转换为Book对象,该对象是要保存的数据模型,而不是Request DTO. (它对任何事物都没有影响,只是不必要的和未使用的).

其次,您在Book数据模型中使用了OrmLite Data Attributes,但没有使用OrmLite插入记录.

要使用OrmLite在ServiceStack Services中插入新的Book,您可以使用:

Db.Insert(postBook);

如果还需要创建Book RDBMS表,可以使用以下方法创建它:

using (var db = dbFactory.Open())
{
    db.CreateTableIfNotExists<Book>();
}

如果尚未在ServiceStack中注册OrmLiteConnectionFactory,则可以通过以下方式注册:

container.Register<IDbConnectionFactory>(c => 
    new OrmLiteConnectionFactory(connString, SqlServer2012Dialect.Provider));

有关更多信息,请参见OrmLite项目页面上的文档:

> https://github.com/ServiceStack/ServiceStack.OrmLite

使用储存库

如果要使用PostBookSqlRepo保存书籍,则应使用IDbConnectionFactory对其进行配置,例如:

public class PostBookSqlRepo : IPostBookRepository
{
    IDbConnectionFactory dbFactory;

    public PostBookSqlRepo(IDbConnectionFactory dbFactory)
    {
        this.dbFactory = dbFactory;
    }

//...

    public Book CreateBooks(Book book)
    {
        using (var db = dbFactory.OpenDbConnection())
        {
            db.Insert(book);
        }
    }
}

您可以使用以下方法在ServiceStack IOC中进行配置:

container.Register<IPostBookRepository>(c =>
    new PostBookSqlRepo(c.Resolve<IDbConnectionFactory>()));
上一篇:C#-ServiceStack的多个路由路径


下一篇:c# – 绕过DotNet核心服务堆栈上的SSL证书验证