开发者

Easy way to refactor table and column names in a Linq .DBML file?

开发者 https://www.devze.com 2023-02-19 05:23 出处:网络
I\'ve recently made a large series of changes to the names of columns and tables within our database (SQL Server).Now I need to update the Linq to SQL Model (.DBML) file to reflect this.

I've recently made a large series of changes to the names of columns and tables within our database (SQL Server). Now I need to update the Linq to SQL Model (.DBML) file to reflect this.

In the past when I've done this; I usually manually rename the column / table then have to go through all instances in the code and rename accordingly. However this is pretty cumbersome as you can imagine.

Is 开发者_开发知识库there an easier way to refactor names in .DBML, something along the lines of "Right Click -> Refactor -> Rename" but for a .DBML file?


This is exactly the reason I chose not to use the dbml way to implement LINQ 2 SQL in my project. I instead defined the classes myself and mapped to tables and columns using attributes.. I know its a lot work. but its worth it..

An example:

    [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");
                }
            }
        }


    }


That there is no "refresh" button for DBML. I typically remove everything, and then drag the views & procedures back from Server Explorer. Be sure to save the DBML after cleaning it, otherwise it will keep the old definitions.


I have used the Huagati DBML Tools for this very purpose, and found it to work very well.

Check it out, it's pretty cheap for a single developer license.

Huagati DBML Tools

0

精彩评论

暂无评论...
验证码 换一张
取 消