我刚刚开始学习servicestack和c#,并且需要一些将数据发布到sql server的帮助。当我使用swagger测试时,收到200响应,但实际上没有任何内容插入数据库,而且我不确定我要去哪里。
Model.Type
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();
}
}
Messages.Request
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; }
}
}
Messages.Response
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;
}
}
}
答案 0 :(得分:4)
首先,您的请求DTO上不应该有[AutoIncrement]
,而是将DTO转换为Book
对象,该对象是您要保存的数据模型,而不是请求DTO。 (它对任何东西都没有任何影响,只是不必要的和未使用的。)
第二,您在Book
数据模型中使用了OrmLite数据属性,但没有使用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项目页面上的文档:
如果您想使用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>()));