I have a problem when i try to cast to class Foo having Bar property. Properties of the class Bar have the same names as the properties of the class Foo:
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public Bar Bar { get; set; }
}
public class Bar
{
public int Id { get; set; }
public string Name { get; set; }
}
Database schema looks like this:
CREATE TABLE dbo.TB_Foo
(
foo_Id INT PRIMARY KEY
,foo_Name NVARCHAR(20)
)
GO
CREATE TABLE dbo.TB_Bar
(
bar_Id INT PRIMARY KEY
,bar_Name NVARCHAR(20)
,bar_FooId INT
,FOREIGN KEY(bar_FooId) REFERENCES dbo.TB_Foo(foo_Id)
)
GO
Sample data:
INSERT INTO dbo.TB_Foo(foo_Id, foo_Name)
VALUES (1, 'Foo1'), (2, 'Foo2'), (3, 'Foo3')
INSERT INTO dbo.TB_Bar(bar_Id, bar_Name, bar_FooId)
VALUES (1, 'Bar1', 1), (2, 'Bar2', 2), (3, 'Bar3', 3)
When i try to use Simple.Data casting to object i get exception:
"System.ArgumentException: An item with the same key has already been adde"
dynamic barAlias;
List<Foo> list = db.TB_Foo
.All()
.With(db.TB_Foo.TB_Bar.As("Bar"), out barAlias)
.Select(
// Columns for properties of class Foo
db.TB_Foo.foo_Id.As("Id"),
db.TB_Foo.foo_Name.As("Name"),
// Columns for properties of class Bar
barAlias.bar_Id.As("Id"),
barAlias.bar_Name.As("Name")
)
.ToList<Foo>();
Is there a way to achieve this? (sorry for my bad english).