。我遇到了linq to sql的問題。Linq to sql,每個人都繼承重複列
我使用來自System.Data.Linq.Mapping的屬性來映射我的類和表。 我注意到,我的代碼中重複了一些列定義(例如ID)。所以我想將這些定義移到基類。我已經寫了這樣的事情:
public abstract class BasicEntity
{
[Column(IsDbGenerated=true, IsPrimaryKey=true)]
public int ID { get; set; }
}
[Table(Name = "Users")]
public class User : BasicEntity
{
[Column(Name = "Name")]
public string Name { get; set; }
}
public class TestDataContext : DataContext
{
public TestDataContext(string connectionString) : base (connectionString) { }
public Table<User> Users { get { return this.GetTable<User>(); } }
}
....
{
TestDataContext context = new TestDataContext(validConnectionString);
if(context.DatabaseExists())
context.DeleteDatabase();
context.CreateDatabase(); // an exception thrown from here
}
所以,當我嘗試創建一個數據庫,我有一個System.Data.SqlClient.SqlException,那就是:「名爲」ID「的列在目標表或視圖中不存在。」 很明顯,這個異常是由我的BasicEntity引起的。
最後,我的問題是:有沒有辦法繼承列定義來刪除重複的代碼?
而且要清楚 - 我使用#develop,它沒有花哨的LINQ2SQL designer/codegenerator,就像VS.
非常感謝提前。
你有多少個實體具有「相同」名稱屬性?我要問的原因,因爲除非它是非常大的數目,否則你所引入的複雜性是不值得的 – 2011-03-17 16:29:39
在一個特定項目中沒有太多的實體,雖然這樣的機制會節省我大量的時間,因爲我會遇到這個在很多未來的項目中都會遇到問 – Dmitriy 2011-03-17 17:23:13