I have MSSQL database with one table:
CREATE TABLE Hierarchy (
Id UNIQUEIDENTIFIER PRIMARY KEY,
version TIMESTAMP NOT NULL,
DiscKey INT NOT NULL,
Enum INT NOT NULL
);
I have objects hierarchy with one base class and two subclasses. I use attributes for mapping objects to database structure:
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;
}
Note: I would like to store EnumA
and EnumB
fields in one column!
In two different units of work, I'm trying: to create object, and to update it afterwards:
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();
}
Problem: the second SubmitChanges()
throws an exception:
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()
But I do not use neither DerivedB, nor EnumB in my code.
Inherite both enum from int . Example;
enum EnumA : int { Foo = 1, Bar = 2 } enum EnumB : int { Xxx = 1, Yyy = 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With