我最近对数据库(SQL Server)中的列和表的名称进行了一系列的更改。现在,我需要更新Linq到SQL模型(.DBML)文件以反映这一点。

过去,当我这样做时;我通常手动重命名列/表,然后必须遍历代码中的所有实例并相应地重命名。但是,您可以想象这很麻烦。

是否有一种更简单的方法来重构.DBML中的名称,类似于
“右键单击->重构->重命名”,但是是否要使用.DBML文件?

最佳答案

这正是我选择不使用dbml方式在项目中实现LINQ 2 SQL的原因。相反,我自己定义了类,并使用属性将其映射到表和列。我知道它的工作量很大。但它值得。

一个例子:

    [Table(Name = "Transaction")]
    public partial class Transaction
    {
        private decimal _amount;
        private int _id
        private DateTime _payDate;
        private int _currencyId;

        private EntityRef<Currency> _currencyMaster;

        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();

        public Transaction()
        {
            _currencyMaster = default(EntityRef<Currency>);
            OnCreated();
        }

       [Column(Storage = "_id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
        public int Id
        {
            get
            {
                return _id;
            }
            protected set
            {
                if ((_id != value))
                {
                    OnPropertyChanging();
                    _id = value;
                    OnPropertyChanged("Id");
                }
            }
        }

        [Column(Storage = "_amount", DbType = "Decimal(18,2) NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public decimal Amount
        {
            get
            {
                return _amount;
            }
            set
            {
                if ((_amount != value))
                {
                    OnPropertyChanging();
                    _amount = value;
                    OnPropertyChanged("Amount");
                }
            }
        }

        [Column(Storage = "_currencyId", DbType = "INT NOT NULL", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
        public int CurrencyId
        {
            get
            {
                return _currencyId;
            }
            set
            {
                if ((_currencyId != value))
                {
                    OnPropertyChanging();
                    _currencyId = value;
                    OnPropertyChanged("CurrencyId");
                }
            }
        }

        [Column(Storage = "_payDate", DbType = "DateTime NOT NULL", UpdateCheck = UpdateCheck.Never)]
        public DateTime PayDate
        {
            get
            {
                return _payDate;
            }
            set
            {
                if ((_payDate != value))
                {
                    OnPropertyChanging();
                    _payDate = value;
                    OnPropertyChanged("PayDate");
                }
            }
        }

        [Association(Name = "Transaction_Currency", Storage = "_currencyMaster", ThisKey = "CurrencyId", OtherKey = "Id", IsForeignKey = true)]
        public Currency Currency
        {
            get
            {
                return _currencyMaster.Entity;
            }
            set
            {
                Currency previousValue = _currencyMaster.Entity;
                if (((previousValue != value)
                            || (_currencyMaster.HasLoadedOrAssignedValue == false)))
                {
                    OnPropertyChanging();
                    if ((previousValue != null))
                    {
                        _currencyMaster.Entity = null;
                        previousValue.Transactions.Remove(this);
                    }
                    _currencyMaster.Entity = value;
                    if ((value != null))
                    {
                        value.Transactions.Add(this);
                        _currencyId = value.Id;
                    }
                    else
                    {
                        _currencyId = default(int);
                    }
                    OnPropertyChanged("Currency");
                }
            }
        }


    }

关于c# - 在Linq .DBML文件中重构表和列名的简便方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5418061/

10-10 07:24