1
Bar 속성을 가진 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; }
}
데이터베이스 스키마는 다음과 같습니다 :
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
샘플 데이터 :
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)
내가 시도 클래스 바의 속성은 Foo 클래스의 속성과 동일한 이름을 가지고 개체에 Simple.Data 캐스팅을 사용하여 예외가 발생합니다.
"System.ArgumentException : 동일한 키를 가진 m은 이미 완료되었습니다. "
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>();
이 방법이 있습니까? (내 하찮은 영어 실력에 죄송하다는 말씀을 드리고 싶습니다).
접두사가 붙은 열에 대한 이유는 모르지만 프로덕션 서버의 모든 테이블에는 다음과 같은 열이 있습니다. - / –