在 Linq .DBML 文件中重构表名和列名的简单方法?

发布于 2024-10-25 08:45:57 字数 235 浏览 1 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苦妄 2024-11-01 08:45:57

这正是我在项目中选择不使用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");
                }
            }
        }


    }

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


    }
人生戏 2024-11-01 08:45:57

DBML 没有“刷新”按钮。我通常会删除所有内容,然后拖动视图和视图。从服务器资源管理器返回的过程。清理后一定要保存 DBML,否则它将保留旧的定义。

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.

书信已泛黄 2024-11-01 08:45:57

我为此目的使用了 Huagati DBML 工具,并且发现它工作得非常好。

看看吧,对于单个开发者许可证来说相当便宜。

华加体DBML工具

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文