C# 如何使LINQtoSQL与抽象模型一起工作?

C# 如何使LINQtoSQL与抽象模型一起工作?,c#,.net,linq-to-sql,model,abstract-class,C#,.net,Linq To Sql,Model,Abstract Class,有一个Linq To Sql模型: abstract class Foo { public int Id { get; set; } public Guid Identifier { get; set; } public string Data { get; set; } } 还有一些后代: sealed class FooInheritor1 : Foo { } sealed class FooInheritor2 : Foo { } sealed class Foo

有一个Linq To Sql模型:

abstract class Foo
{
    public int Id { get; set; }
    public Guid Identifier { get; set; }
    public string Data { get; set; }
}
还有一些后代:

sealed class FooInheritor1 : Foo { }
sealed class FooInheritor2 : Foo { }
sealed class FooInheritor3 : Foo { }
我想通过Linq to Sql从数据库中检索数据,并将其存储回数据库

为了实现这一点,我将通过将结果转换为非抽象继承器来查询数据库,如下所示:

var foos = dataContext.Foos.Where(someCondition).Cast<FooInheritor1>().ToArray();
但是,提交时会出现一个例外情况:

System.Data.Linq.dll中发生“System.NullReferenceException”类型的异常,但未在用户代码中处理。 其他信息:对象引用未设置为对象的实例

我在谷歌上搜索了一下,发现如果我使用指定的
IsDiscriminator
列对模型应用一些层次结构属性,那么linqtosql可能会满足我的需要。但它告诉我,
Guid
typed属性不能是鉴别器列

然后,我在模型上创建了一个类型为
string
的伪属性,并用该
ColumnAttribute
标记它,但出现了另一个错误,说明它无法将列插入数据库。事实证明,linqtosql将尝试这样做,并且将失败,除非我在数据库表中也创建了那个伪列


最后,我如何实现我想要的功能?我如何告诉它我只希望模型是抽象的(因为它应该是抽象的),并且我可以以正确的方式处理它?

经过多次尝试,我发现没有办法实现我想要实现的目标


我以某种特定的方式做了最好的变通,这绝对不是广大观众所喜欢的。

也许你可以看看这篇文章@Rex I did,它对我没有帮助。
dataContext.Foos.InsertOnSubmit(new FooInheritor1());
dataContext.Foos.InsertOnSubmit(new FooInheritor2() { Data = "Hi" });