2008-10-13 2 views
1

스키마가 초기화중인 개체와 일치하는지 확인하려고합니다.C#에서 ActiveRecord 클래스의 테이블 이름을 말할 수 있습니까?

단순히 클래스 이름을 반영하는 것 이외의 클래스의 TableName을 가져 오는 방법이 있습니까?

나는 명시 적으로 테이블 이름

와 함께 몇 가지 클래스를 사용하고

편집 : 난 당신이 테이블 이름을 지정하지 않을 경우 추가 조의 솔루션을 사용하여, 아마 당신이 만약 제약

public string find_table_name(object obj) 
{ 
     object[] attribs = obj.GetType().GetCustomAttributes(typeof(Castle.ActiveRecord.ActiveRecordAttribute), false); 

     if (attribs != null) 
     { 
      ActiveRecordAttribute attrib = (Castle.ActiveRecord.ActiveRecordAttribute) attribs[0]; 
      if (attrib.Table != null) 
       return attrib.Table; 
      return obj.GetType().Name; 
     } 
    return null; 
} 

답변

3

를 사용할 수 있습니다

[ActiveRecord(Table = "NewsMaster")] 
public class Article 
{ 
    [PrimaryKey(Generator = PrimaryKeyType.Identity)] 
    public int NewsId { get; set; } 

    [Property(Column = "NewsHeadline")] 
    public string Headline { get; set; } 

    [Property(Column = "EffectiveStartDate")] 
    public DateTime StartDate { get; set; } 

    [Property(Column = "EffectiveEndDate")] 
    public DateTime EndDate { get; set; } 

    [Property] 
    public string NewsBlurb { get; set; } 
} 

이 당신을 얻을 것이다 테이블 이름 :

,369 다음과 같은이
[Test] 
    public void Can_get_table_name() 
    { 
     var attribs = typeof(Article).GetCustomAttributes(typeof(Castle.ActiveRecord.ActiveRecordAttribute), false); 

     if (attribs != null) 
     { 
      var attrib = (Castle.ActiveRecord.ActiveRecordAttribute) attribs[0]; 
      Assert.AreEqual("NewsMaster", attrib.Table); 
     } 
    } 
+0

내가 –

+0

이이 역으로 수행 할 수 있습니다 그것을 테스트거야 좋아 보인다 참조? IE : (ModelClassName [] all_tag = ActiveRecordBase ) .FindAll();가 될 수 있도록 ModelClassName 모듈을 가져오고 싶습니다. ?? 나는이 같은 해결책을 거쳐서 거쳐야 할 필요가 있다고 생각하는 또 다른 질문을하고있다. 희망은 그 말이 맞습니다. 여기 질문이 있으면. http://stackoverflow.com/q/8332556/746758 –

2

은 또한 사용할 수 있습니다 :

ActiveRecordModel.GetModel(typeof(Article)).ActiveRecordAtt.Table 

this testcase

+0

이것은 모델 테이블 이름에 액세스하는 올바른 방법입니다. – SyntaxGoonoo

관련 문제