C# 乐:
即:C# 乐:,c#,asp.net,mapping,dapper,C#,Asp.net,Mapping,Dapper,即: …但在某些情况下,我可能不想填充phototegory对象…-在这些情况下,您当然可以始终创建虚拟的phototegory对象,只需设置它们的Id。当我只需要传递标识符,但我的方法需要对象实例时,我通常会这样做。为什么不在Dapper的GitHub repo上提出一个问题呢?更有可能的是,您会更快地得到服务,项目所有者的问题也会被记录下来。因为这似乎很普遍。我自己的表通常与相同的列名相关。可能是Hi Sam的重复。这是个好消息,谢谢。此更新版本何时显示在屏幕上?我现在在那里看不到吗?谢谢山
…但在某些情况下,我可能不想填充
phototegory
对象…-在这些情况下,您当然可以始终创建虚拟的phototegory
对象,只需设置它们的Id
。当我只需要传递标识符,但我的方法需要对象实例时,我通常会这样做。为什么不在Dapper的GitHub repo上提出一个问题呢?更有可能的是,您会更快地得到服务,项目所有者的问题也会被记录下来。因为这似乎很普遍。我自己的表通常与相同的列名相关。可能是Hi Sam的重复。这是个好消息,谢谢。此更新版本何时显示在屏幕上?我现在在那里看不到吗?谢谢山姆,一切都解决了:)就像我当时想的那样。谢谢你的修复。
PhotoId (PK - int)
CategoryId (FK - smallint)
UserId (int)
CategoryId (PK - smallint)
CategoryName (nvarchar(50))
public class Photo
{
public int PhotoId { get; set; }
public short CategoryId { get; set; }
public int UserId { get; set; }
public PhotoCategory PhotoCategory { get; set; }
}
public class PhotoCategory
{
public short CategoryId { get; set; }
public string CategoryName { get; set; }
{
var sql = @"select p.*, c.* from Photos p inner
join PhotoCategories c
on p.CategoryID = c.CategoryID where p.PhotoID = @pid";
cn.Open();
var myPhoto = cn.Query<Photo, PhotoCategory, Photo>(sql,
(photo, photoCategory) => { photo.PhotoCategory = photoCategory;
return photo; },
new { pid = photoID }, null, true, splitOn: "CategoryID").Single();
class Program
{
static void Main(string[] args)
{
var createSql = @"
create table #Users (UserId int, Name varchar(20))
create table #Posts (Id int, OwnerId int, Content varchar(20))
insert #Users values(99, 'Sam')
insert #Users values(2, 'I am')
insert #Posts values(1, 99, 'Sams Post1')
insert #Posts values(2, 99, 'Sams Post2')
insert #Posts values(3, null, 'no ones post')
";
var sql =
@"select * from #Posts p
left join #Users u on u.UserId = p.OwnerId
Order by p.Id";
using (var connection = new SqlConnection(@"CONNECTION STRING HERE"))
{
connection.Open();
connection.Execute(createSql);
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post; }, splitOn: "UserId");
var apost = data.First();
apost.Content = apost.Content;
connection.Execute("drop table #Users drop table #Posts");
}
}
}
class User
{
public int UserId { get; set; }
public string Name { get; set; }
}
class Post
{
public int Id { get; set; }
public int OwnerId { get; set; }
public User Owner { get; set; }
public string Content { get; set; }
}
class Program
{
static void Main(string[] args)
{
var createSql = @"
create table #Users (UserId int, Name varchar(20))
create table #Posts (Id int, UserId int, Content varchar(20))
insert #Users values(99, 'Sam')
insert #Users values(2, 'I am')
insert #Posts values(1, 99, 'Sams Post1')
insert #Posts values(2, 99, 'Sams Post2')
insert #Posts values(3, null, 'no ones post')
";
var sql =
@"select * from #Posts p
left join #Users u on u.UserId = p.UserId
Order by p.Id";
using (var connection = new SqlConnection(@"CONNECTION STRING HERE"))
{
connection.Open();
connection.Execute(createSql);
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post; }, splitOn: "UserId");
var apost = data.First();
apost.Content = apost.Content;
connection.Execute("drop table #Users drop table #Posts");
}
}
}
class User
{
public int UserId { get; set; }
public string Name { get; set; }
}
class Post
{
public int Id { get; set; }
public int UserId { get; set; }
public User Owner { get; set; }
public string Content { get; set; }
}
class Foo1
{
public int Id;
public int BarId { get; set; }
}
class Bar1
{
public int BarId;
public string Name { get; set; }
}
public void TestMultiMapperIsNotConfusedWithUnorderedCols()
{
var result = connection.Query<Foo1,Bar1,
Tuple<Foo1,Bar1>>(
"select 1 as Id, 2 as BarId, 3 as BarId, 'a' as Name",
(f,b) => Tuple.Create(f,b), splitOn: "BarId")
.First();
result.Item1.Id.IsEqualTo(1);
result.Item1.BarId.IsEqualTo(2);
result.Item2.BarId.IsEqualTo(3);
result.Item2.Name.IsEqualTo("a");
}
classes: A{Id,FooId} B{FooId,Name}
splitOn: "FooId"
data: Id, FooId, FooId, Name
SELECT
user.Name, user.Email, user.AddressId As id, address.*
FROM
User user
Join Address address
ON user.AddressId = address.AddressId