我最近对数据库(SQL Server)中列和表的名称进行了大量更改。现在我需要更新Linq to SQL Model(.DBML)文件以反映这一点。
过去我做过这件事;我通常手动重命名列/表,然后必须遍历代码中的所有实例并相应地重命名。然而,这是非常麻烦的,你可以想象。
是否有一种更简单的方法来重构.DBML中的名称 “右键单击 - >重构 - >重命名”但对于.DBML文件?
答案 0 :(得分:2)
这正是我选择不使用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");
}
}
}
}
答案 1 :(得分:1)
DBML没有“刷新”按钮。我通常删除所有内容,然后拖动视图&amp;从Server Explorer返回的过程。确保在清理DBML后保存它,否则它将保留旧的定义。
答案 2 :(得分:0)