我有一个带有一张表的 MSSQL 数据库:
CREATE TABLE Hierarchy (
Id UNIQUEIDENTIFIER PRIMARY KEY,
version TIMESTAMP NOT NULL,
DiscKey INT NOT NULL,
Enum INT NOT NULL
);
我有一个基类和两个子类的对象层次结构。我使用属性将对象映射到数据库结构:
enum ObjectType { A = 1, B = 2 }
enum EnumA { Foo = 1, Bar = 2 }
enum EnumB { Xxx = 1, Yyy = 2 }
[Table(Name = "Hierarchy")]
[InheritanceMapping(Code = ObjectType.A, Type = typeof(DerivedA), IsDefault = true)]
[InheritanceMapping(Code = ObjectType.B, Type = typeof(DerivedB))]
class Base
{
[DataMember, Column(Name = "Id", DbType = "UniqueIdentifier", CanBeNull = false, IsPrimaryKey = true)]
public Guid Id;
[Column(Name = "DiscKey", DbType = "int", CanBeNull = false, IsDiscriminator = true)]
public ObjectType DiscKey;
[Column(Name = "version", DbType = "RowVersion", CanBeNull = false, IsVersion = true, IsDbGenerated = true)]
public byte[] Version { get; protected set; }
}
class DerivedA : Base
{
[DataMember, Column(Name = "Enum", DbType = "int", CanBeNull = false)]
public EnumA Enum;
}
class DerivedB : Base
{
[DataMember, Column(Name = "Enum", DbType = "int", CanBeNull = false)]
public EnumB Enum;
}
注意:我想在一列中存储EnumA
和EnumB
字段!
在两个不同的工作单元中,我正在尝试:创建对象,然后更新它:
var objId = Guid.NewGuid();
var o1 = new DerivedA {Id = objId, Enum = EnumA.Foo};
using (var context = new DataContext(connectionString))
{
context.GetTable(typeof(Base)).InsertOnSubmit(o1);
context.SubmitChanges();
}
var o2 = new DerivedA {Id = objId, Enum = EnumA.Bar};
using (var context = new DataContext(connectionString))
{
context.GetTable(typeof(Base)).Attach(o2, true);
context.SubmitChanges();
}
问题:第二个SubmitChanges()
抛出异常:
Unhandled Exception: System.ArgumentException: The argument 'value' was the wrong type. Expected 'ConsoleApplication8.EnumA'. Actual 'ConsoleApplication8.EnumB'.
at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitAssign(SqlAssign sa)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitUpdate(SqlUpdate update)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlVisitor.VisitBlock(SqlBlock b)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitIncludeScope(SqlIncludeScope scope)
at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
at System.Data.Linq.SqlClient.SqlBinder.Bind(SqlNode node)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicUpdate(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Update(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at ConsoleApplication8.Program.Main()
但我的代码中既不使用 DerivedB,也不使用 EnumB。