0

この質問から、対応するプロパティとの ColumnAttribute 関連付けを実装しましたが、Linq to SQL がこのプロパティを列にマップしようとしても機能しません。

私たちのプロパティとマッピングコード(他の質問から):

    public System.Xml.Linq.XElement Name {
        get {
            return this.name;
        }
        set {
            this.OnNameChanging(value);
            this.SendPropertyChanging();
            this.name = value;
            this.SendPropertyChanged("Name");
            this.OnNameChanged();
        }
    }

        System.Data.Linq.Mapping.ColumnAttribute columnAttribute = new System.Data.Linq.Mapping.ColumnAttribute();
        columnAttribute.Name = "Name";
        columnAttribute.Storage = "name";
        columnAttribute.DbType = "Xml NOT NULL";
        columnAttribute.CanBeNull = false;
        columnAttribute.UpdateCheck = System.Data.Linq.Mapping.UpdateCheck.Never;

        PropertyOverridingTypeDescriptor propertyOverrideTypeDescriptor = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(typeof(ClassToMap)).GetTypeDescriptor(typeof(ClassToMap)));
        PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(ClassToMap)).Cast<PropertyDescriptor>().ToArray().Where(prop => prop.Name == "Name").FirstOrDefault();

        PropertyDescriptor pd2 = TypeDescriptor.CreateProperty(
            typeof(ClassToMap).GetType(),
            pd, // base property descriptor to which we want to add attributes
            // The PropertyDescriptor which we'll get will just wrap that
            // base one returning attributes we need.
            columnAttribute
            // this method really can take as many attributes as you like, not just one
        );

        propertyOverrideTypeDescriptor.OverrideProperty(pd2);
        TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(typeof(ClassToMap)), typeof(ClassToMap));

テーブル マッピングを更新する方法はありますか?

4

1 に答える 1

0

リフレクションは型記述子を利用しません。コード内のクラスに別の属性が追加されているため、テストに合格した可能性があります。

その場で LINQ to SQL クラス マッピングを変更する方法があります。その秘訣は、XML マッピング ファイルをリソースに追加し、データ コンテキストを作成するときにそれを使用することです。XML データをメモリにロードすると、必要に応じてマッピングを変更できます。このソリューションについては、動的テーブルを使用した Linq to sqlで詳しく説明しています。

コード スニペット:

foreach (XElement col in xe.Descendants().Where(e => e.Name.LocalName == "Column")) {
    XAttribute name = col.Attributes().FirstOrDefault(a => a.Name.LocalName == "Name" && a.Value == "CategoryName");
    if (name != null)
        name.Value = "Name";
}
于 2013-06-19T01:11:23.807 に答える