1

我想知道是否可以为 linq 类动态设置表映射:

    [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Comfort 0601$Contact]")]
    public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged
    {
     ...

我想动态或以编程方式更改它的原因是因为我使用的数据库是由 Navision 创建的。那里的表格有这些表格所属的公司名称的前缀。因此,我希望能够更改该前缀。

    [global::System.Data.Linq.Mapping.TableAttribute(Name = "dbo.["+SPResources.Database.Company+"$Contact]")]

惠特公司为:

    public const string Company = "Comfort 0601";

我已经尝试过了,但只有当我将 Company 声明为常量时它才有效。

公司之间的表结构是相同的。只是名称和内容更改。

我希望有人可以对此提出一些建议。我也不确定这是否可能。

4

4 回答 4

2

认为添加表前缀的正确解决方案是使用 SQL Server 2005(或更高版本),然后为每个所需的前缀创建一个 db-user,并将每个 db-user 的默认架构从“dbo”更改为唯一的。

另一种解决方案是扩展现有的 DataContext ,使其能够在运行时“更正” MappingSource:

[Table]  // Look no name
public class DbRecord
{
    [Column(IsPrimaryKey=true)]
    public long Id { get; set; }
}

CustomDataContext.UpdateCustomTable(typeof(DbRecord), "DbTable");

using (CustomDataContext dc = new CustomDataContext(dbConnection))
{
    Table<DbRecord> dbTable = dc.GetTable<DbRecord>();
    var query = from item in dbTable;
}

这是自定义的 DataContext (我想可以很容易地更改它以向现有表名添加前缀)(如果在多线程环境中,请用 ConcurrentDictionary 替换 Dictionary):

public class CustomDataContext : DataContext
{
    static CustomMappingSource _sharedMappingSource = new CustomMappingSource();

    public static void UpdateCustomTable(Type rowType, string tableName)
    {
        _sharedMappingSource.UpdateCustomTable(rowType, tableName);
    }

    public CustomDataContext(System.Data.IDbConnection connection) : base(connection, _sharedMappingSource) { }
    public CustomDataContext(string fileOrServerOrConnection) : base(fileOrServerOrConnection, _sharedMappingSource) { }
}

internal class CustomMappingSource : MappingSource
{
    AttributeMappingSource mapping = new AttributeMappingSource();

    Dictionary<Type, string> _customTableNames = new Dictionary<Type, string>();

    public void UpdateCustomTable(Type rowType, string tableName)
    {
        if (string.IsNullOrEmpty(tableName))
            throw new ArgumentNullException("TableName");

        _customTableNames[rowType] = tableName;
    }

    protected override MetaModel CreateModel(Type dataContextType)
    {
        MetaModel oldmodel = mapping.GetModel(dataContextType);
        CustomMetaModel newmodel = new CustomMetaModel(oldmodel, _customTableNames);
        return newmodel;
    }
}

internal class CustomMetaModel : MetaModel
{
    MetaModel _orgmodel;

    Dictionary<Type, MetaTable> _customtables = new Dictionary<Type, MetaTable>();
    Dictionary<Type, string> _tableNames = new Dictionary<Type, string>();

    public CustomMetaModel(MetaModel orgmodel, Dictionary<Type, string> tableNames)
    {
        _orgmodel = orgmodel;
        _tableNames = tableNames;
    }

    public override MetaType GetMetaType(Type type)
    {
        MetaTable metaTable;
        if (_customtables.TryGetValue(type, out metaTable))
            return metaTable.RowType;
        else
            return _orgmodel.GetMetaType(type);
    }
    public override MetaTable GetTable(Type rowType)
    {
        MetaTable customMetaTable;
        if (_customtables.TryGetValue(rowType, out customMetaTable))
            return customMetaTable;

        if (_tableNames.ContainsKey(rowType))
        {
            MetaTable orgtable = _orgmodel.GetTable(rowType);
            MetaType orgrowtype = orgtable.RowType;
            CustomMetaType newRowType = new CustomMetaType(orgrowtype, this);
            _customtables.Add(rowType, new CustomMetaTable(orgtable, this, newRowType, _tableNames[rowType]));
            newRowType.MetaTable = _customtables[rowType];
            return newRowType.MetaTable;
        }

        return _orgmodel.GetTable(rowType);
    }

    #region MetaModel Forwards
    public override Type ContextType { get { return _orgmodel.ContextType; } }
    public override string DatabaseName { get { return _orgmodel.DatabaseName; } }
    public override MappingSource MappingSource { get { return _orgmodel.MappingSource; } }
    public override Type ProviderType { get { return _orgmodel.ProviderType; } }
    public override MetaFunction GetFunction(System.Reflection.MethodInfo method) { return _orgmodel.GetFunction(method); }
    public override IEnumerable<MetaFunction> GetFunctions() { return _orgmodel.GetFunctions(); }
    public override IEnumerable<MetaTable> GetTables() { return _orgmodel.GetTables(); }
    #endregion
}

internal class CustomMetaTable : MetaTable
{
    MetaTable _orgtable;
    MetaModel _metamodel;
    MetaType _rowtype;
    string _tableName;

    public CustomMetaTable(MetaTable orgtable, MetaModel metamodel, MetaType rowtype, string tableName)
    {
        _orgtable = orgtable;
        _metamodel = metamodel;
        _rowtype = rowtype;
        _tableName = tableName;
    }

    public override MetaModel Model { get { return _metamodel; } }
    public override MetaType RowType { get { return _rowtype; } }
    public override string TableName { get { return _tableName; } }

    #region MetaTable Forwards
    public override System.Reflection.MethodInfo DeleteMethod { get { return _orgtable.DeleteMethod; } }
    public override System.Reflection.MethodInfo InsertMethod { get { return _orgtable.InsertMethod; } }
    public override System.Reflection.MethodInfo UpdateMethod { get { return _orgtable.UpdateMethod; } }
    #endregion
}

internal class CustomMetaType : MetaType
{
    MetaType _orgtype;
    MetaModel _metamodel;

    public MetaTable MetaTable { get; set; }

    public CustomMetaType(MetaType orgtype, MetaModel metamodel)
    {
        _orgtype = orgtype;
        _metamodel = metamodel;
    }

    public override MetaTable Table { get { return MetaTable; } }
    public override MetaModel Model { get { return _metamodel; } }

    #region MetaType Forwards
    public override System.Collections.ObjectModel.ReadOnlyCollection<MetaAssociation> Associations { get { return _orgtype.Associations; } }
    public override bool CanInstantiate { get { return _orgtype.CanInstantiate; } }
    public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> DataMembers { get { return _orgtype.DataMembers; } }
    public override MetaDataMember DBGeneratedIdentityMember { get { return _orgtype.DBGeneratedIdentityMember; } }
    public override System.Collections.ObjectModel.ReadOnlyCollection<MetaType> DerivedTypes { get { return _orgtype.DerivedTypes; } }
    public override MetaDataMember Discriminator { get { return _orgtype.Discriminator; } }
    public override bool HasAnyLoadMethod { get { return _orgtype.HasAnyLoadMethod; } }
    public override bool HasAnyValidateMethod { get { return _orgtype.HasAnyValidateMethod; } }
    public override bool HasInheritance { get { return _orgtype.HasInheritance; } }
    public override bool HasInheritanceCode { get { return _orgtype.HasInheritanceCode; } }
    public override bool HasUpdateCheck { get { return _orgtype.HasUpdateCheck; } }
    public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> IdentityMembers { get { return _orgtype.IdentityMembers; } }
    public override MetaType InheritanceBase { get { return _orgtype.InheritanceBase; } }
    public override object InheritanceCode { get { return _orgtype.InheritanceCode; } }
    public override MetaType InheritanceDefault { get { return _orgtype.InheritanceDefault; } }
    public override MetaType InheritanceRoot { get { return _orgtype.InheritanceRoot; } }
    public override System.Collections.ObjectModel.ReadOnlyCollection<MetaType> InheritanceTypes { get { return _orgtype.InheritanceTypes; } }
    public override bool IsEntity { get { return _orgtype.IsEntity; } }
    public override bool IsInheritanceDefault { get { return _orgtype.IsInheritanceDefault; } }
    public override string Name { get { return _orgtype.Name; } }
    public override System.Reflection.MethodInfo OnLoadedMethod { get { return _orgtype.OnLoadedMethod; } }
    public override System.Reflection.MethodInfo OnValidateMethod { get { return _orgtype.OnValidateMethod; } }
    public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> PersistentDataMembers { get { return _orgtype.PersistentDataMembers; } }
    public override Type Type { get { return _orgtype.Type; } }
    public override MetaDataMember VersionMember { get { return _orgtype.VersionMember; } }
    public override MetaDataMember GetDataMember(System.Reflection.MemberInfo member) { return _orgtype.GetDataMember(member); }
    public override MetaType GetInheritanceType(Type type) { return _orgtype.GetInheritanceType(type); }
    public override MetaType GetTypeForInheritanceCode(object code) { return _orgtype.GetTypeForInheritanceCode(code); }
    #endregion
}
于 2013-07-30T14:25:39.593 回答
1

Brian 和 Gert 的答案是这个问题的可能解决方案。但是在运行时更改映射是不可能的(正如布赖恩提到的)。

随着我们项目的进展,我们决定如果我们需要另一个公司名称(这意味着该构建是针对不同的客户),我们将重新构建该项目。该决定使以下解决方案成为可能:

我们注释掉了 Visual Studio 生成的原始映射:

//[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Comfort 0601$Contact]")]
    public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged
    {
    ...

因为我们对每个 Linq 类都有自己的部分类,所以我们在自己的类的顶部添加了修改后的映射:

[global::System.Data.Linq.Mapping.TableAttribute(Name = "dbo.["+SPResources.Database.Company+"$Contact]")]
public partial class Contact
{

我们公司将在哪里:

public const string Company = "Comfort 0601";

如果其他公司需要重建,我们只需更改 Company 变量。

注意:这仅在您不更改 VS 中的 *.dbml 文件时才能正常工作。如果这样做,VS 将自动撤消您的更改。

编辑:随着时间的流逝,我开始更多地研究实体框架,我找到了一个更具体和更适合这个问题的解决方案:http: //www.codeproject.com/Articles/421643/How-to-Use-MVC- Net-on-the-Dynamics-NAV-Database-St

于 2013-01-16T11:02:10.207 回答
0

如果您首先使用代码,则可以使用流利的映射,您可以使用该ToTable()方法在运行时配置数据库表(请参阅更改数据库表名称下)。例如,在OnModelCreating()您的 DbContext 派生的覆盖中:

string company = "Comfort 0601";
modelBuilder.Entity<Contact>().ToTable(company + "Contact");
modelBuilder.Entity<....
modelBuilder.Entity<....

您可以从配置文件中获取公司名称。

于 2013-01-07T19:37:11.830 回答
0

您可以使用SQLMetal为每个实体自定义生成代码。您不能在运行时动态应用属性,因此这将是最佳选择。

于 2013-01-07T13:21:52.080 回答