C# 创建可继承的LINQ到SQL可映射类
我正在为Windows Phone 8构建一个库,它需要本地数据库。出于显而易见的原因,库的用户将创建一个可映射的LINQ到SQL类,该类具有适当的C# 创建可继承的LINQ到SQL可映射类,c#,linq,windows-phone-8,sql-server-ce,C#,Linq,Windows Phone 8,Sql Server Ce,我正在为Windows Phone 8构建一个库,它需要本地数据库。出于显而易见的原因,库的用户将创建一个可映射的LINQ到SQL类,该类具有适当的[表]s和[列]s。然而,要在每一个这样的类中使用,我需要更多的列来实现库的内部功能。我的想法是在库中包含一个基类,它将具有与所需列对应的成员。用户只需从该类继承,添加自己的成员,并将该类用作最终的LINQ到SQL映射 到目前为止,我的基类如下所示: //Class to contain all the essential members [Tabl
[表]
s和[列]
s。然而,要在每一个这样的类中使用,我需要更多的列来实现库的内部功能。我的想法是在库中包含一个基类,它将具有与所需列对应的成员。用户只需从该类继承,添加自己的成员,并将该类用作最终的LINQ到SQL映射
到目前为止,我的基类如下所示:
//Class to contain all the essential members
[Table]
public class SyncableEntityBase : NotifyBase, ISyncableBase
{
[Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
public int ItemId { get; set; }
[Column]
public bool IsDeleted { get; set; }
[Column]
public DateTime RemoteLastUpdated { get; set; }
[Column]
public DateTime LocalLastUpdated { get; set; }
}
[Table]
public class ToDoCategory : SyncableEntityBase
{
private string _categoryName;
[Column]
public string CategoryName
{
get
{
return _categoryName;
}
set
{
if (_categoryName != value)
{
NotifyPropertyChanging("CategoryName");
_categoryName = value;
NotifyPropertyChanged("CategoryName");
}
}
}
private string _categoryColor;
[Column]
public string CategoryColor
{
get
{
return _categoryColor;
}
set
{
if (_categoryColor != value)
{
NotifyPropertyChanging("CategoryColor");
_categoryColor = value;
NotifyPropertyChanged("CategoryColor");
}
}
}
}
还有派生类,类似这样:
//Class to contain all the essential members
[Table]
public class SyncableEntityBase : NotifyBase, ISyncableBase
{
[Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
public int ItemId { get; set; }
[Column]
public bool IsDeleted { get; set; }
[Column]
public DateTime RemoteLastUpdated { get; set; }
[Column]
public DateTime LocalLastUpdated { get; set; }
}
[Table]
public class ToDoCategory : SyncableEntityBase
{
private string _categoryName;
[Column]
public string CategoryName
{
get
{
return _categoryName;
}
set
{
if (_categoryName != value)
{
NotifyPropertyChanging("CategoryName");
_categoryName = value;
NotifyPropertyChanged("CategoryName");
}
}
}
private string _categoryColor;
[Column]
public string CategoryColor
{
get
{
return _categoryColor;
}
set
{
if (_categoryColor != value)
{
NotifyPropertyChanging("CategoryColor");
_categoryColor = value;
NotifyPropertyChanged("CategoryColor");
}
}
}
}
这个想法是让最后一堂课有四个基本列,两个由用户添加。根据MSDN文档,我需要附加
[InheritanceMapping]
,它需要继承的类型。但是,在我构建库时,我无法知道用户将从我的基类派生什么类型(以及有多少类型)。这有什么办法吗?如何使用?也许您可以使用它:@ErikEJ这种方法基本上强制用户实现属性。用户仍然需要做一些不必要的工作,因为属性应该绝对存在,用户不会做任何更改。其次,我只是编写了接口而不是抽象类。比较起来如何?我认为您正在进行一项不可能完成的任务,可能使用代码生成方法?(我可以帮你)@ErikEJ我同意任何可行的方法。最终的想法是通过一个图书馆来实现。在上面的链接中,这个家伙似乎能够很愉快地使用SQLite完成我正在做的事情。此外,他还有CreateTable
方法可以解决我的问题。。。