Có, có questions here và here về cách chèn bản ghi với dapper-dot-net. Tuy nhiên, câu trả lời, trong khi thông tin, dường như không chỉ cho tôi đi đúng hướng. Đây là tình huống: di chuyển dữ liệu từ SqlServer sang MySql. Đọc các hồ sơ vào một IEnumerable<WTUser>
thật dễ dàng, nhưng tôi chỉ không nhận được một cái gì đó trên chèn. Trước tiên, 'mã hồ sơ di chuyển':Cách chèn bộ sưu tập <T> IEnumerable với dapper-dot-net
// moving data
Dim session As New Session(DataProvider.MSSql, "server", _
"database")
Dim resources As List(Of WTUser) = session.QueryReader(Of WTUser)("select * from tbl_resource")
session = New Session(DataProvider.MySql, "server", "database", _
"user", "[email protected]$$w0rd")
// *edit* - corrected parameter notation with '@'
Dim strInsert = "INSERT INTO tbl_resource (ResourceName, ResourceRate, ResourceTypeID, ActiveYN) " & _
"VALUES (@ResourceName, @ResourceRate, @ResourceType, @ActiveYN)"
Dim recordCount = session.WriteData(Of WTUser)(strInsert, resources)
// session Methods
Public Function QueryReader(Of TEntity As {Class, New})(ByVal Command As String) _
As IEnumerable(Of TEntity)
Dim list As IEnumerable(Of TEntity)
Dim cnn As IDbConnection = dataAgent.NewConnection
list = cnn.Query(Of TEntity)(Command, Nothing, Nothing, True, 0, CommandType.Text).ToList()
Return list
End Function
Public Function WriteData(Of TEntity As {Class, New})(ByVal Command As String, ByVal Entities As IEnumerable(Of TEntity)) _
As Integer
Dim cnn As IDbConnection = dataAgent.NewConnection
// *edit* if I do this I get the correct properties, but no data inserted
//Return cnn.Execute(Command, New TEntity(), Nothing, 15, CommandType.Text)
// original Return statement
Return cnn.Execute(Command, Entities, Nothing, 15, CommandType.Text)
End Function
cnn.Query và cnn.Execute gọi các phương thức mở rộng dapper. Bây giờ, lớp WTUser (lưu ý: tên cột thay đổi từ 'WindowsName' trong SQLServer để 'ResourceName' trong MySql, do đó hai thuộc tính trỏ đến cùng lĩnh vực):
Public Class WTUser
// edited for brevity - assume the following all have public get/set methods
Public ActiveYN As String
Public ResourceID As Integer
Public ResourceRate As Integer
Public ResourceType As Integer
Public WindowsName As String
Public ResourceName As String
End Class
Tôi nhận được một ngoại lệ từ đoan trang : "WTUser không được Dapper hỗ trợ". Phương pháp này trong DataMapper (đoan trang):
private static Action<IDbCommand, object> CreateParamInfoGenerator(Type OwnerType)
{
string dmName = string.Format("ParamInfo{0}", Guid.NewGuid());
Type[] objTypes = new[] { typeof(IDbCommand), typeof(object) };
var dm = new DynamicMethod(dmName, null, objTypes, OwnerType, true); // << - here
// emit stuff
// dm is instanced, now ...
foreach (var prop in OwnerType.GetProperties().OrderBy(p => p.Name))
Tại thời điểm này OwnerType =
System.Collections.Generic.List`1 [[CRMBackEnd.WTUser, CRMBE, Version = 1.0.0.0, Văn hóa = trung tính, PublicKeyToken = null]], mscorlib, Version = 2.0.0.0, Culture = trung tính, PublicKeyToken = b77a5c561934e089
dường như OwnerType phải là CRMBackEnd.WTUser
... không phải List<CRMBackEnd.WTUser>
... ??? bởi vì những gì đang xảy ra là các thuộc tính thu thập đang được lặp lại: Đếm, Năng lực, vv Tôi đang thiếu gì?
Cập nhật
Nếu tôi sửa đổi session.WriteData như:
Public Function WriteData(Of TEntity As {Class, New})(ByVal Command As String, _
ByVal Entities As IEnumerable(Of TEntity)) _
As Integer
Dim cnn As IDbConnection = dataAgent.NewConnection
Dim records As Integer
For Each entity As TEntity In Entities
records += cnn.Execute(Command, entity, Nothing, 15, CommandType.Text)
Next
Return records
End Function
hồ sơ được chèn độc đáo ... nhưng tôi không nghĩ rằng đây sẽ là ví dụ được đưa cần thiết như:
connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3"
... từ dapper-dot-net
Chỉnh sửa WTUser này: tất cả các trường 'Công khai * * đều là getter/setter ... Tôi đã chỉnh sửa vì lý do ngắn gọn: VB hoàn toàn tiết lộ. Tôi sẽ làm rõ bài viết. ;) – IAbstract
@Sam là giải pháp chung chung này? tức là nó hoạt động trên Oracle? – MaYaN
Làm cách nào để lấy lại ID cho các thực thể mà tôi chèn? –